利用AFN上传图片(多张)

43 篇文章 0 订阅
#pragma mark 上传图片  
+ (void)uploadImageWithPath:(NSString *)path photos:(NSArray *)photos params:(NSDictionary *)params success:(HttpUploadSuccessBlock)success failure:(HttpUploadFailureBlock)failure  
{  
    [SVProgressHUD showProgress:-1 status:@"正在上传,请稍等."];  
    path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  
    AFHTTPSessionManager *manager = [HttpTool sharedHttpSession];  
    [manager POST:path parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {  
        for (int i = 0; i < photos.count; i ++) {  
            NSDateFormatter *formatter=[[NSDateFormatter alloc]init];  
            formatter.dateFormat=@"yyyyMMddHHmmss";  
            NSString *str=[formatter stringFromDate:[NSDate date]];  
            NSString *fileName=[NSString stringWithFormat:@"%@.jpg",str];  
            UIImage *image = photos[i];  
            NSData *imageData = UIImageJPEGRepresentation(image, 0.28);  
            [formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"upload%d",i+1] fileName:fileName mimeType:@"image/jpeg"];  
        }  
    } progress:^(NSProgress * _Nonnull uploadProgress) {  
        MyLog(@"uploadProgress is %lld,总字节 is %lld",uploadProgress.completedUnitCount,uploadProgress.totalUnitCount);  
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {  
        NSString *resultCode = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"result_code"]];  
        NSString *resultInfo = [responseObject objectForKey:@"result_info"];  
        MyLog(@"resultInfo is %@",resultInfo);  
        if ([resultCode isEqualToString:@"1"]) {  
            [SVProgressHUD showSuccessWithStatus:@"上传成功"];  
            if (success == nil) return ;  
            success(responseObject);  
        }else {  
            [SVProgressHUD showErrorWithStatus:resultInfo];  
            if (failure == nil) return ;  
            failure();  
        }  
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {  
        [SVProgressHUD showErrorWithStatus:@"上传失败"];  
        if (failure == nil) return ;  
        failure();  
    }];  
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值