如何使用AFNetworking通过POST发送图像作为参数(参数) [英] How to send image as parameters (param) with POST with AFNetworking
本文介绍了如何使用AFNetworking通过POST发送图像作为参数(参数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将图像作为参数发送,例如
I need to send image as param like
URl : some API
params : {profileImage:string(file)}
仅在参数列表中,我必须将图像文件作为字符串发送。
Means in param list only i have to send image file as string.
我使用了以下代码。但它不起作用。
i used the below code. but it is not working.
NSData *dataImage = [[NSData alloc] init];
dataImage = UIImagePNGRepresentation(selectedImage);
NSString *stringImage = [dataImage base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSDictionary *params = {profileImage : stringImage}
NSString *url = [NetworkRoutes postProfileImageAPIWithMobileNumber:[PTUserDetails getMobileNumber]];
self.operationManager = [AFHTTPSessionManager manager];
self.operationManager.responseSerializer = [AFJSONResponseSerializer serializer]; //
[self.operationManager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"userName" password:@"some password"];
[self.operationManager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
NSError *error;
if (![formData appendPartWithFileURL:[NSURL fileURLWithPath:path] name:@"file" fileName:[path lastPathComponent] mimeType:@"image/jpg" error:&error]) {
NSLog(@"error appending part: %@", error);
}
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
您的答案无需进行网络连接,也可以进行nsurlconnection
your answer no need to be in afnetworking , can also be in nsurlconnection
我要重新存储
{
响应:请上传图像文件
}
I am getting resposne { response :"Please upload image file" }
或
在所附的屏幕截图中,建议我如何操作。在邮递员中,我得到响应
Suggest me how to do like in the attached screen shot . In post man i am getting response
推荐答案
NSData *imgData = UIImageJPEGRepresentation(image, 1.0);
NSUInteger fileSize = [imgData length];
if(fileSize>400000)
{
float size = (float)((float)400000/(float)fileSize);
imgData = [NSData dataWithData:UIImageJPEGRepresentation(image, size)];
}
NSString *imgProfilePic = [imgData base64Encoding];
and then you can send this imgProfilePic to Webservice
这篇关于如何使用AFNetworking通过POST发送图像作为参数(参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文