使用AFNetworking将图像与其他参数一起发送 [英] send image along with other parameters with AFNetworking
问题描述
我正在使用 AFNetworking
更新使用 ASIHTTPRequest
的旧应用程序代码。在我的情况下,我将数据发送到 API
,这些数据是不同的类型:图像和其他。
<这是我到目前为止采用的代码,实现API客户端,请求共享实例,准备params字典并将其发送到远程API:
NSMutableDictionary * params = [NSMutableDictionary dictionary];
[params setValue:@some valueforKey:aKey];
[[APIClient sharedInstance]
postPath:@/ post
参数:params成功:^(AFHTTPRequestOperation * operation,id responseObject){
// some逻辑
}失败:^(AFHTTPRequestOperation *操作,NSError *错误){
//处理错误
}];
当我想将图像添加到参数时会出现什么情况
字典?
使用 ASIHTTPRequest
,我曾经做过以下事情:
NSData * imgData = UIImagePNGRepresentation(anImage);
NSString * newStr = [anImageName stringByReplacingOccurrencesOfString:@/
withString:@_];
[请求addData:imgData
withFileName:[NSString stringWithFormat:@%@。png,newStr]
andContentType:@image / png
forKey:anOtherKey];
我深入研究 AFNetworking
文档并发现它们将图像附加到 NSMutableRequest
中,如下所示:
AFHTTPClient * httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSData * imageData = UIImageJPEGRepresentation([UIImage imageNamed:@avatar.jpg],0.5);
NSMutableURLRequest * request = [httpClient multipartFormRequestWithMethod:@POST路径:@/ upload参数:nil constructBodyWithBlock:^(id< AFMultipartFormData> formData){
[formData appendPartWithFileData:imageData name:@ avatarfileName:@avatar.jpgmimeType:@image / jpeg];
}];
我应该如何将我的图像数据整合到<$ c $中以简洁的方式将它们混合在一起c> APIClient 请求?提前完成。
我使用相同的AFNetworking上传带有一些参数的图像。这段代码很适合我。也许它会有所帮助
NSData * imageToUpload = UIImageJPEGRepresentation(uploadedImgView.image,1.0); //(uploadedImgView.image) ;
if(imageToUpload)
{
NSDictionary * parameters = [NSDictionary dictionaryWithObjectsAndKeys:keyParameter,@keyName,nil];
AFHTTPClient * client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@http:// ------]];
NSMutableURLRequest * request = [client multipartFormRequestWithMethod:@POSTpath:@API name as you haveparameters:parameters constructBodyWithBlock:^(id< AFMultipartFormData> formData){
[ formData appendPartWithFileData:imageToUpload name:@imagefileName:@temp.jpegmimeType:@image / jpeg];
}];
AFHTTPRequestOperation * operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[操作setCompletionBlockWithSuccess:^(AFHTTPRequestOperation * operation,id responseObject)
{
NSDictionary * jsons = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
// NSLog(@response:%@,jsons);
}
失败:^(AFHTTPRequestOperation *操作,NSError *错误)
{
if([operation.response statusCode] == 403)
{
// NSLog(@上传失败);
返回;
}
// NSLog(@错误:%@,[操作错误]);
}];
[操作开始];
}
祝你好运!
I am updating an old application code which used ASIHTTPRequest
with AFNetworking
. In my case, I am sending a bench of data to API
, these data are different types: Image and other.
Here is the code I adopt so far, implementing an API client, requesting a shared instance, prepare the params dictionary and send it to remote API:
NSMutableDictionary *params = [NSMutableDictionary dictionary];
[params setValue:@"Some value" forKey:aKey];
[[APIClient sharedInstance]
postPath:@"/post"
parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//some logic
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//handle error
}];
What would be the case when I want to add an image to the params
dictionary?
With ASIHTTPRequest
, I used to do the following:
NSData *imgData = UIImagePNGRepresentation(anImage);
NSString *newStr = [anImageName stringByReplacingOccurrencesOfString:@"/"
withString:@"_"];
[request addData:imgData
withFileName:[NSString stringWithFormat:@"%@.png",newStr]
andContentType:@"image/png"
forKey:anOtherKey];
I digged into AFNetworking
documentation and found they appending the image in an NSMutableRequest
like this:
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"avatar.jpg"], 0.5);
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
}];
How should I mix this together on a neat way to integrate my image data into the APIClient
request? Thanx in advance.
I have used same AFNetworking to upload image with some parameter. This code is fine working for me. May be it will help out
NSData *imageToUpload = UIImageJPEGRepresentation(uploadedImgView.image, 1.0);//(uploadedImgView.image);
if (imageToUpload)
{
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:keyParameter, @"keyName", nil];
AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://------"]];
NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"API name as you have" parameters:parameters constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData: imageToUpload name:@"image" fileName:@"temp.jpeg" mimeType:@"image/jpeg"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSDictionary *jsons = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
//NSLog(@"response: %@",jsons);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
if([operation.response statusCode] == 403)
{
//NSLog(@"Upload Failed");
return;
}
//NSLog(@"error: %@", [operation error]);
}];
[operation start];
}
Good Luck !!
这篇关于使用AFNetworking将图像与其他参数一起发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!