图片上传时必要将图片进行压缩,不然会上传失败
上传一张图片
[manager POST:urlString parameters:params constructingBodyWithBlock:^(id_Nonnull formData) {
//使用日期生成图片名称
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString *fileName = [NSString stringWithFormat:@"%@.png",[formatter stringFromDate:[NSDate date]]];
[formData appendPartWithFileData:imageData name:@"uploadFile" fileName:fileName mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
//上传图片成功执行回调
} failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
//上传图片失败执行回调
}];
[manager POST:urlString parameters:params constructingBodyWithBlock:^(id_Nonnull formData) {
NSInteger imgCount = 0;
for (NSData *imageData in imageDatas) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss:SSS";
NSString *fileName = [NSString stringWithFormat:@"%@%@.png",[formatter stringFromDate:[NSDate date]],@(imgCount)];
[formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"uploadFile%@",@(imgCount)] fileName:fileName mimeType:@"image/png"];
imgCount++;
}
} success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
} failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
}];
原文链接:http://www.jianshu.com/p/0e28fdef0f91