ios – 使用AFNetworking发送图像以及其他参数

我正在更新使用ASIHTTPRequest与AFNetworking的旧应用程序代码.在我的情况下,我正在向API发送一个数据库,这些数据是不同的类型:图像和其他.

以下是我现在采用的代码,实现API客户端,请求共享实例,准备params字典并将其发送到远程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

 }];

当我想要添加一个图像到params字典会是什么情况?

使用ASIHTTPRequest,我曾经做过以下事情:

NSData *imgData = UIImagePNGRepresentation(anImage);

Nsstring *newStr = [anImageName stringByReplacingOccurrencesOfString:@"/"
                                                              withString:@"_"];



[request 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" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
}];

我应该如何混合在一起整合我的图像数据到apiclient请求? Thanx提前.

解决方法

我已经使用相同的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:@"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];
}

祝你好运 !!

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...