在发送到服务器之前使用UIImageJPEGRepresentation压缩图像

时间:2015-07-27 23:45:57

标签: ios objective-c uiimagejpegrepresentation

我尝试在使用UIImageJPEGRepresentation上传到服务器之前压缩图像。

到目前为止,我已将图像更改为PNG并将质量更改为0.01f。我尝试了几种不同的写作方法。

在发送到服务器之前这是最佳的吗?我应该改变什么来优化?

 - (void)uploadPhoto {
    WUTModelImageUploadReq *imageUploadReq = [[WUTModelImageUploadReq alloc]init];

    imageUploadReq.photo = [self encodeToBase64String:[UIImage imageWithData:UIImageJPEGRepresentation(self.viewControllerPost.imageForPost, 0.01f)]];
    imageUploadReq.extension = @"png";

    void (^wsSuccessHandler)(AFHTTPRequestOperation *operation, NSDictionary* responseObject) = ^(AFHTTPRequestOperation *operation, id responseObject){
    NSLog(@"Pull Feed responseObject %@",responseObject);

    NSError *error;
    WUTModelPostImageResponse *wsResponse = [[WUTModelPostImageResponse alloc]initWithDictionary:(NSDictionary *)responseObject error:&error];

    if (error) {
        errorMessage = @"Failure to upload image.";
        [self postExecuteFail];
    }
    else{
        if (wsResponse.success) {
            WUTModelImage *imageTemp = [wsResponse.data firstObject];
            [postItem setObject:imageTemp.photo forKey:@"photo"];
            [self uploadPostFeed];

        }else{
            errorMessage = @"Failure to upload image.";
            [self postExecuteFail];
        }
      }
   };

1 个答案:

答案 0 :(得分:0)

建议:

  1. 使用扩展程序" jpg"而不是" png"。
  2. 使用0.0表示compressionQuality以最大化压缩。
  3. 如果您的情况允许,请考虑缩小图片大小。