带有数据和图像的 AFNetworking 3.x 发布请求
Posted
技术标签:
【中文标题】带有数据和图像的 AFNetworking 3.x 发布请求【英文标题】:AFNetworking 3.x post request with data and image 【发布时间】:2016-04-04 16:45:42 【问题描述】:我想向我的后端发送一个发布请求,其中包含一些数据和一个 UIImage 作为 NSData 对象。问题是,我不知道如何使用 AFNetworking 3.0。
到目前为止我的代码:
NSString *url = [NSString stringWithFormat:@"%@%@", baseURL, @"/postProjectNote"];
NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
[dic setObject:session forKey:@"session"];
[dic setObject:timestamp forKey:@"timestamp"];
[dic setObject:project_id forKey:@"project_id"];
[dic setObject:type forKey:@"type"];
NSData imagedata = UIImageJPEGRepresentation(myUIImage, 0.8);
我不需要任何进度条。如果请求成功与否,我只需要一个结果。后端(Laravel 5)给了我一个 json 字符串。我需要将其与表单数据一起发送。
你能帮我开始吗?
【问题讨论】:
【参考方案1】:使用此代码通过 AFNetworking 发布图片:
AFHTTPRequestOperationManager* manager = [[AFHTTPRequestOperationManager alloc] init];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
NSMutableDictionary *paramDict = [NSMutableDictionary new]; // Add additional parameters here
AFHTTPRequestOperation *op = [manager POST:UPDATE_PROFILE_IMAGE parameters:paramDict constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
[formData appendPartWithFileData:imageData name:@"file" fileName:@"filename" mimeType:@"image/jpeg"];
success:^(AFHTTPRequestOperation *operation, id responseObject)
if (success)
// Success
failure:^(AFHTTPRequestOperation *operation, NSError *error)
// Failure
];
[op start];
【讨论】:
这适用于 AFNetworking 2.x 而不是 3.x。但是使用迁移教程很容易隐藏。【参考方案2】: NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager.requestSerializer setValue:token forHTTPHeaderField:@"Authorization"];
[manager.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
manager.responseSerializer.acceptableContentTypes =[NSSet setWithObjects:@"text/html",@"application/json",nil];
[manager POST:encoded parameters:"the params you want to pass" constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData)
[formData appendPartWithFileData:imageData
name:"image name with timestamp"
fileName:@"image_upload_file"
mimeType:[NSString mimeTypeForImageData:data]];
progress:^(NSProgress * _Nonnull uploadProgress)
//DLog(@"Progress = %@",uploadProgress);
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject)
//DLog(@"Response = %@",responseObject);
completion(YES,responseObject,nil);
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
completion(NO,nil,error);
//DLog(@"Error: %@", error);
];
【讨论】:
以上是关于带有数据和图像的 AFNetworking 3.x 发布请求的主要内容,如果未能解决你的问题,请参考以下文章
AFNetworking - 向 REST 发送 HTTP POST,发送带有 Base64string 图像的 JSON
UITableViewCell AFNetworking 仅加载一次带有动画的图像
如何使用 AFNetworking 从一系列 URL 下载和存储图像?