2016-11-30 59 views
0

我有要求將圖像傳遞到Web服務。我正在使用AFNetworking。我的代碼到目前爲止在下面給出。解析圖像扔AFNetworking

NSData *imageData = UIImageJPEGRepresentation(_imgA.image, 0.5); 
params = @{@"category":self.displayDept.text, 
      @"location": locationAdress.text, 
      @"topic":self.topic.text, 
      @"image":imageData, 
      @"email":[NSString stringWithFormat:@"%@",useEmail] 
      }; 



AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; 
[manager POST:@"webservice....." 

    parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 
     [formData appendPartWithFormData:imageData name:@"imageData"]; 

    } success:^(NSURLSessionTask *task, id responseObject) 

有人能說出這段代碼有什麼問題嗎?謝謝

+0

您正在添加圖像兩次?一個在參數和多部分?這兩個時間使用不同的密鑰? – Wolverine

+0

哪一個會在這裏工作?參數還是多部分?你能爲此建議一種方法嗎? @Wolverine – Ramanan

+0

Multipart將工作。從參數中刪除。 – Wolverine

回答

0

用於從pickercontroller獲取圖像並將圖像上傳到服務器。

UIImage *img = [info valueForKey:UIImagePickerControllerOriginalImage]; 
    NSData *imgData = UIImageJPEGRepresentation(image, 0.5); 
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
    NSDictionary *parameters = @{@"foo": @"bar"}; 
    [manager POST:@"<json link>" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 
     [formData appendPartWithFormData:imgData name:@"image"]; 
    } success:^(AFHTTPRequestOperation *operation, id responseObject) { 
     NSLog(@"Success: %@", responseObject); 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Error: %@", error); 
    }]; 

希望這解決了您的問題。