美文网首页三方管理实用工具iOS技术中心
实战--AFNetworking上传一张或多张图片

实战--AFNetworking上传一张或多张图片

作者: 晨阳聊电影 | 来源:发表于2016-08-06 15:33 被阅读2621次

    本人也是小白,最近在做公司自己的项目,我就把项目中实用的我自己总结了一下,希望能帮助到和我一样刚开发的人,这一次总结得是用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);
     NSDictionary *datas = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
            
            //
            NSLog(@"请求成功%@",datas);
            
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
        
    
        
        ```
        
    ## 上传多张图片 
    

    NSArray * _imageArr = @[@"1",@"2",@"3"];

    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:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        for (int i=0; i<_imageArr.count; i++) {
        
            
        UIImage * image =[UIImage  imageNamed:_imageArr[i]];
        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);
    

    NSDictionary *datas = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];

        //
        NSLog(@"请求成功%@",datas);
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
     ```

    相关文章

      网友评论

      本文标题:实战--AFNetworking上传一张或多张图片

      本文链接:https://www.haomeiwen.com/subject/dyvksttx.html