zoukankan      html  css  js  c++  java
  • IOS开发-图片上传

       目前IOS端开发,图片上传到服务器分为两种,一种是直接上到服务器,一种是借助第三方储存(减少服务器压力)。

    一、直接上传到服务器

     1         /**
     2          *  代码演示
     3          */
     4         //*******UIImagePNGRepresentation(UIImage* image) 要比UIImageJPEGRepresentation(UIImage* image, 1.0) 返回的图片数据量大很多,但从视角角度看,图片的质量没有明显的降低,所以在读取图片数据内容时,可以先使用UIImageJPEGRepresentation,且耗时短。
     5         //******将选中的图片压缩成二进制文件*******photo为UIImage实例
     6         NSData *data = UIImageJPEGRepresentation(photo, 0.5);
     7         //****** 从本地上传图片  先读入沙盒目录
     8          //****** 创建沙盒目录
     9         NSString *docomentpaths = [NSHomeDirectory() stringByAppendingPathComponent:@"docomentpaths"];
    10         NSFileManager *manager = [NSFileManager defaultManager];
    11         [manager createDirectoryAtPath:docomentpaths withIntermediateDirectories:YES attributes:nil error:nil];
    12         //*****获取当前时间字符
    13         NSString *timeStr = [NSString stringWithFormat:@"%@",[NSDate date]];
    14         //*****读入沙盒目录下
    15         [manager createFileAtPath:[docomentpaths stringByAppendingString:[NSString stringWithFormat:@"%@",timeStr]] contents:data attributes:nil];
    16         //*****得到图片路径
    17         NSString *filepath = [[NSString alloc]initWithFormat:@"%@%@",docomentpaths,timeStr];
    18          //****** 得到本地的图片URL
    19         NSURL *imageurl = [NSURL fileURLWithPath:filepath];
    20         
     1  //****url服务器接口地址
     2     //使用AFN网络框架上传
     3

             AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]init];

     4     [manager POST:url parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
     5         //         开始上传 formData提交
     6            [formData appendPartWithFileURL:imageurl name:@"ios_uploadImage" fileName:filepath mimeType:@"image/jpeg" error:nil];
     7                  }
     8 
     9         //      这里上传多张图片for循环提交
    10         //        for (int i = 0; i<self.imagelist.count; i++)
    11         //         {
    12         //             [formData appendPartWithFileURL:self.iamgedata[i] name:@"heareuy" fileName:self.strlist[i] mimeType:@"image/jpeg" error:nil];
    13         //         }
    14         //
    15         
    16     } success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
    17         
    18     } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
    19         
    20     }];

    二、上传到第三方服务器存储-七牛云存储

    原来公司的项目也是上传到公司服务器上,但想着图片量会很大,随着图片量的增多,后期也是一个问题,所以我决定使用七牛云作为图片存储盘,过程其实是非常简单的。后台的伙伴们会在七牛上注册,实例化一个盘,在手机端上传时,后台会给一个从七牛获取到token。根据这个token手机端会把图片上传到七牛,上传成功后,七牛会返回一个图片的url,然后再将这个url给服务器,说白了,服务器只是保存手机端上传到七牛的地址。

    首先导入七牛云的SDK,拷贝七牛云给demo里面的方法.如下:

     1 //********需要上传的图片
     2     NSData *data = UIImageJPEGRepresentation(imag, 1.0f);
     3     //    ***********用时间戳来给图片命名
     4     NSDateFormatter *formatter;
     5     NSString        *dateString;
     6     formatter = [[NSDateFormatter alloc] init];
     7     [formatter setDateFormat:@"yyyyMMddhhmmss"];
     8     dateString = [formatter stringFromDate:[NSDate date]];
     9     dateString =[dateString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    10     //        NSString *token = @"从服务端SDK获取";
    11     //******七牛云申请的域名
    12     NSString *qiniuIP = @"http://oddgox1e4.bkt.clouddn.com/";
    13     QNUploadManager *upManager = [[QNUploadManager alloc] init];
    14         [upManager putData:data key:dateString token:token
    15                   complete: ^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
    16                       NSLog(@"info=%@", info);
    17                       NSLog(@"key=%@", key);
    18                          NSLog(@"resp=%@", resp);
    19         [self uploadiamg:[NSString stringWithFormat:@"%@%@",qiniuIP,key]];
    20                   
    21             } option:nil];
    uploadiamg:自己写上传服务器的方法。搞定。
  • 相关阅读:
    != 比 & 的优先级高
    VC++ 报错:Heap corruption detected
    sqlite 查询数据库中所有的表名,判断某表是否存在,将某列所有数值去重后获得数量
    Unicode与UTF-8,UTF-16
    各种格式的压缩包解压,7zip 命令行
    bat批处理文件运行时隐藏cmd窗口
    标准库中 vector list等排序
    duilib 实现列表头任意拖动
    duilib 实现 XML重用(item完全重合的CList)
    C++-POJ2503-Babelfish[hash]
  • 原文地址:https://www.cnblogs.com/chenhongios/p/5998906.html
Copyright © 2011-2022 走看看