美文网首页iOS
iOS开发中利用AFN上传图片

iOS开发中利用AFN上传图片

作者: 不疯魔难以成佛 | 来源:发表于2016-08-11 23:29 被阅读0次

    使用AFN上传一张或多张图片
    -(void)upLoadImage:(NSMutableDictionary *)parametersDic url:(NSString *)strMethod image:(UIImage *)image requestTag:(int)tag withObj:(id)obj andImage2:(UIImage *)image2
    {
    //parametersDic 请求的参数 strMethod:请求的方法 image上传的第一张图片 image2:上传的第二张图片 obj:nil tag :1
    //定义数组用来保存多张图片
    NSArray *dataArray =nil;
    //将图片转换为data
    NSData *imagedata= UIImageJPEGRepresentation(image, 0.2);//0.2为压缩倍数
    NSData *imagedata2= UIImageJPEGRepresentation(image2,0.2);
    NSString *format = @"jpg";
    NSString *Suffix = @"jpeg";
    //判断是png jpg格式
    if (imagedata == nil || imagedata2 == nil) {
    imagedata = UIImagePNGRepresentation(image);
    imagedata2 = UIImagePNGRepresentation(image2);
    format = @"png";
    Suffix = @"png";
    }
    //判断有几张图片
    if (image2 == nil) {
    dataArray = @[imagedata];
    }else{

        dataArray= @[imagedata,imagedata2];
    }
    

    //对应的URL
    NSString *urltmps=[[NSString stringWithFormat:@"%@%@.action",Host_url,strMethod] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    //创建请求
    NSMutableURLRequest *request=[[AFHTTPRequestSerializer serializer]multipartFormRequestWithMethod:@"POST" URLString:urltmps parameters:parametersDic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    //多张图片的时候才使用循环,单张 图片不用
    for (NSInteger i = 0; i<dataArray.count; i++) {
    //获取当前日期时间作为文件名称
    NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
    formatter.dateFormat=@"yyyyMMddHHmmss";
    NSString *str=[formatter stringFromDate:[NSDate date]];

        NSString *fileName=[NSString stringWithFormat:@"%@%zd.%@",str,i+1,format];
    

    //对应接口文档上传的图片的类型名称,这里是file1和file2
    NSString *file = [NSString stringWithFormat:@"file%zd",i+1];
    [formData appendPartWithFileData:dataArray[i] name:file fileName:fileName mimeType:[NSString stringWithFormat:@"image/%@",Suffix]];
    }
    } error:nil];

    AFURLSessionManager *manager=[[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    NSURLSessionUploadTask *uploadTask=[manager uploadTaskWithStreamedRequest:request progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error)
        {
          //  [self RequestResult:nil requestTag:tag withObj:obj];
        NSLog(@“上传失败”);
        }
        else
        {
            //json解析
            NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
            
           // [self RequestResult:resultDic requestTag:tag withObj:obj];
            
        }
    }];
    

    //执行,没有这句是不会执行的
    [uploadTask resume];

    }

    相关文章

      网友评论

        本文标题:iOS开发中利用AFN上传图片

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