• AFNetworking上传一张或多张图片,并压缩图片节约占用内存


          最近在做APP的时候,遇到了难题:根据公司需求,在用户评论并上传图片的时候,有的手机像素比较高拍的照片高清的,但是每张图片占用的内存太大,或者上传照片的时候,相册的部分照片本身就占很大内存空间,后台数据库内存量有限,如果后期用户达到了几百万,每个用户都评论并上传这么高像素大内存的图片上去,后台数据库怕承受不了,继续拓展后台存储量的话,需要很大成本,那么怎么在前台来处理这些上传的图片而又不失大体呢?

        这次总结得是用AFNetworking 向后台传输一张或多张图片,并且将照片压缩,感觉还是AFNetworking好使,相比原生的NSSURLsession的传输方法,话不多说,开始分享,用的话,代码基本不用改,直接用~(上传头像的时候,点击保存~触发这个)

    上传单张图片:

     

     UIImage *image = [UIImage  imageNamed:@"3"];
        NSMutableDictionary *photo = [NSMutableDictionary dictionary];
        [photo setObject:@"123" forKey:@"phoneNumber"];
    
        [photo setObject:@"123" forKey:@"psw"];
    
        NSLog(@"1111%@",photo);
    
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.requestSerializer.timeoutInterval = 20;
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithArray:@[@"text/plain",@"multipart/form-data"]];
        [manager POST:@"http://192.168.0.105:8080/miracle/Upload.action" parameters:photo constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
    
            NSDate *date = [NSDate date];
            NSDateFormatter *formormat = [[NSDateFormatter alloc]init];
            [formormat setDateFormat:@"HHmmss"];
            NSString *dateString = [formormat stringFromDate:date];
    
            NSString *fileName = [NSString  stringWithFormat:@"%@.png",dateString];
            NSData *imageData = UIImageJPEGRepresentation(image, 1);
            double scaleNum = (double)300*1024/imageData.length;
            NSLog(@"图片压缩率:%f",scaleNum);
    
            if(scaleNum <1){
    
                imageData = UIImageJPEGRepresentation(image, scaleNum);
            }else{
    
                imageData = UIImageJPEGRepresentation(image, 0.1);
    
            }
    
            [formData  appendPartWithFileData:imageData name:@"files" fileName:fileName mimeType:@"image/jpg/png/jpeg"];
    
        } progress:^(NSProgress * _Nonnull uploadProgress) {
    
            NSLog(@"---%@",uploadProgress);
    
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    
            NSLog(@"`````````%@",responseObject);
    
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    
        }];
    View Code

    上传多张图片:

     1 NSArray * _imageArr = @[@"1",@"2",@"3"];
     2 
     3 
     4     AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
     5     manager.requestSerializer.timeoutInterval = 20;
     6     manager.responseSerializer.acceptableContentTypes = [NSSet setWithArray:@[@"text/plain",@"multipart/form-data"]];
     7     [manager POST:@"http://192.168.0.105:8080/miracle/Upload.action" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
     8 
     9         for (int i=0; i<_imageArr.count; i++) {
    10 
    11 
    12         UIImage * image =[UIImage  imageNamed:_imageArr[i]];
    13         NSDate *date = [NSDate date];
    14         NSDateFormatter *formormat = [[NSDateFormatter alloc]init];
    15         [formormat setDateFormat:@"HHmmss"];
    16         NSString *dateString = [formormat stringFromDate:date];
    17 
    18         NSString *fileName = [NSString  stringWithFormat:@"%@.png",dateString];
    19         NSData *imageData = UIImageJPEGRepresentation(image, 1);
    20         double scaleNum = (double)300*1024/imageData.length;
    21         NSLog(@"图片压缩率:%f",scaleNum);
    22 
    23 
    24 
    25         if(scaleNum <1){
    26 
    27             imageData = UIImageJPEGRepresentation(image, scaleNum);
    28         }else{
    29 
    30             imageData = UIImageJPEGRepresentation(image, 0.1);
    31 
    32         }
    33 
    34         [formData  appendPartWithFileData:imageData name:@"files" fileName:fileName mimeType:@"image/jpg/png/jpeg"];
    35         }
    36     } progress:^(NSProgress * _Nonnull uploadProgress) {
    37 
    38         NSLog(@"---%@",uploadProgress);
    39 
    40     } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    41 
    42         NSLog(@"`````````%@",responseObject);
    43 
    44 
    45     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    46 
    47 
    48     }];
  • 相关阅读:
    hdu4738(双连通分量)
    hdu4635(强连通缩点)
    hdu4612(双连通缩点+树的直径)
    poj3177(边双连通分量+缩点)
    poj3694(tarjan缩点+lca)
    hdu5171(矩阵快速幂)
    uva796(求桥数目)
    uva315(求割点数目)
    POJ1058 The Gourmet Club
    POJ1057 FILE MAPPING
  • 原文地址:https://www.cnblogs.com/liuzhi20101016/p/5757180.html
Copyright © 2020-2023  润新知