Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用AFN网络发送图像和其他参数_Ios_Afnetworking - Fatal编程技术网

Ios 使用AFN网络发送图像和其他参数

Ios 使用AFN网络发送图像和其他参数,ios,afnetworking,Ios,Afnetworking,我正在用AFNetworking更新使用ASIHTTPRequest的旧应用程序代码。在我的例子中,我向API发送了一组数据,这些数据有不同的类型:Image和other 以下是我到目前为止采用的代码,实现API客户端,请求共享实例,准备params字典并将其发送到远程API: NSMutableDictionary *params = [NSMutableDictionary dictionary]; [params setValue:@"Some value" forKey:aKey];

我正在用
AFNetworking
更新使用
ASIHTTPRequest
的旧应用程序代码。在我的例子中,我向
API
发送了一组数据,这些数据有不同的类型:Image和other

以下是我到目前为止采用的代码,实现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"];
}];
AFHTTPClient*httpClient=[[AFHTTPClient alloc]initWithBaseURL:url];
NSData*imageData=UIImageJPEG表示法([UIImage ImageName:@“avatar.jpg”],0.5);
NSMutableURLRequest*request=[httpClient multipartFormRequestWithMethod:@“POST”路径:@“/upload”参数:nil constructingBodyWithBlock:^(id formData){
[formData appendPartWithFileData:imageData名称:@“avatar”文件名:@“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];
}
NSData*imageToUpload=uiimagejpeg表示(上传edimgview.image,1.0)//(上传edimgview.image);
如果(imageToUpload)
{
NSDictionary*参数=[NSDictionary Dictionary WithObjectsAndKeys:keyParameter,@“keyName”,nil];
AFHTTPClient*客户端=[AFHTTPClient客户端WithBaseURL:[NSURL URLWithString:@]http://------"]];
NSMutableURLRequest*request=[客户端multipartFormRequestWithMethod:@“POST”路径:@“API名称如您所用”参数:参数constructingBodyWithBlock:^(id formData){
[formData appendPartWithFileData:ImageToPload名称:@“image”文件名:@“temp.jpeg”mimeType:@“image/jpeg”];
}];
AFHTTPRequestOperation*操作=[[AFHTTPRequestOperation alloc]initWithRequest:request];
[操作setCompletionBlockWithSuccess:^(AFHTTPRequestOperation*操作,id响应对象)
{
NSDictionary*jsons=[NSJSONSerialization JSONObjectWithData:responseObject选项:针状体错误:nil];
//NSLog(@“响应:%@”,JSON);
}
失败:^(AFHTTPRequestOperation*操作,NSError*错误)
{
如果([operation.response statusCode]==403)
{
//NSLog(@“上传失败”);
返回;
}
//NSLog(@“错误:%@,[操作错误]);
}];
[操作启动];
}

祝你好运

在AFNetworking 2.0.1中,这段代码对我很有用

-(void) saveImage: (NSData *)imageData forImageName: (NSString *) imageName {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    NSString *imagePostUrl = [NSString stringWithFormat:@"%@/v1/image", BASE_URL];
    NSDictionary *parameters = @{@"imageName": imageName};

    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:imagePostUrl parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:imageData name:@"image" fileName:imageName mimeType:@"image/jpeg"];
    }];

    AFHTTPRequestOperation *op = [manager HTTPRequestOperationWithRequest:request success: ^(AFHTTPRequestOperation *operation, id responseObject) {
        DLog(@"response: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        DLog(@"Error: %@", error);
    }];
    op.responseSerializer = [AFHTTPResponseSerializer serializer];
    [[NSOperationQueue mainQueue] addOperation:op];
}
而不是

op.responseSerializer = [AFHTTPResponseSerializer serializer];

您好,您将图像的
键放在哪里?Thanx.@“image”是您要上载的任何图像的键。我应该在path参数中特别添加什么?AFHTTPClient被拆分为。它在2.0中不再存在。
op.responseSerializer = [AFHTTPResponseSerializer serializer];