【问题标题】:AFNetworking upload image with PUT request?AFNetworking 使用 PUT 请求上传图片?
【发布时间】:2014-04-07 09:55:17
【问题描述】:

我正在尝试使用 AFNetworkingPUT 请求将图像上传到服务器。-

     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),尽管我正在尝试压缩图像? 我做错了什么?任何建议或示例代码将不胜感激。

【问题讨论】:

  • 为了什么?调整源图像的大小?
  • 源图像的大小是多少?上传 10 / 12 MB 的图像需要一些时间。您唯一的选择是限制 WiFi 上传/调整图像大小以使数据更小
  • 我正在从 iPhone 相机拍摄图像并将其上传到服务器。所以不知道大小。
  • 我已经编辑了问题,请看一下。

标签: ios objective-c afnetworking-2


【解决方案1】:
NSData *imageData = UIImageJPEGRepresentation(snap, 1.0);

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

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

尝试将其减少到平衡图像质量和上传速度的数字。

【讨论】:

    【解决方案2】:
    // 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];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-30
      • 1970-01-01
      相关资源
      最近更新 更多