AFNetworking上传图片与PUT请求?

时间:2014-04-07 09:55:18

标签: ios objective-c afnetworking-2

我尝试使用带有AFNetworking请求的PUT将图片上传到服务器.-

     UIImage* snap = info[UIImagePickerControllerOriginalImage];
      NSData *imageData = UIImageJPEGRepresentation(snap, 0.3);
      NSMutableString * fullPath = [NSMutableString stringWithString:API_BASE_URL];
    [fullPath appendFormat:@"%@%@",API_VERSION,req];
    NSURL * url = [NSURL URLWithString:fullPath];

   AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:url];

    [manager.requestSerializer setValue:[NSString stringWithFormat:@"%@", @kPublicKey] forHTTPHeaderField:@"X-API-KEY"];
    [manager.requestSerializer setValue:bodyStr forHTTPHeaderField:@"X-API-DATA"];
    NSString *URLString = fullPath;

    NSMutableURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"PUT" URLString:URLString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:imageData name:@"media" fileName:@"upload.jpg" mimeType:@"image/jpeg"];
    } error:nil];


   AFHTTPRequestOperation *requestOperation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {

      //success
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

         NSLog(@"failure...");

    }];

    [requestOperation start];

我使用iPhone相机拍摄图像并将其上传到服务器但是处理过程需要花费太多时间,上传到服务器上的图像尺寸很大(~10-12MB),尽管我试图压缩图片? 我做错了什么?任何建议或示例代码都会受到赞赏。

2 个答案:

答案 0 :(得分:0)

NSData *imageData = UIImageJPEGRepresentation(snap, 1.0);

UIImageJPEGRepresentation中的第二个参数表示图像的压缩质量。根据Apple文档:

  

compressionQuality:
  生成的JPEG图像的质量,表示为0.0到1.0之间的值。值0.0表示最大压缩(或最低质量),而值1.0表示压缩程度最低(或最佳质量)。

尝试将此缩小为一个数字,以平衡图像质量和上传速度。

答案 1 :(得分:0)

// manager needs to be init'd with a valid baseURL
NSURL *baseURL = [AfarHTTPSessionManager sharedManager].baseURL;
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL];

NSData *imageData = UIImageJPEGRepresentation(draftHighlight.largeImage, 1);

// need to pass the full URLString instead of just a path like when using 'PUT' or 'POST' convenience methods
NSString *URLString = [NSString stringWithFormat:@"%@%@", baseURL, _the_rest_of_your_path];
NSMutableURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"PUT" URLString:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:imageData name:kCreateHighlightAPIKeyImage fileName:@"highlight_image.jpg" mimeType:@"image/jpeg"];
}];

// 'PUT' and 'POST' convenience methods auto-run, but HTTPRequestOperationWithRequest just
// sets up the request. you're responsible for firing it.
AFHTTPRequestOperation *requestOperation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
    // success
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // failure
}];

// fire the request
[requestOperation start];