1

I'm trying to compress an image before uploading to the server using UIImageJPEGRepresentation.

So far, I've changed the image to PNG and changed the quality to 0.01f. I've tried a few different ways of writing this.

Is this optimal before sending to the server? What should I change to optimize?

 - (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];
        }
      }
   };
Paul
  • 1,179
  • 3
  • 14
  • 38
  • 1
    Your using UIImageJPEGRepresentation, but giving the file the extension "png". Why? – picciano Jul 27 '15 at 23:50
  • Also, not sure what you mean by optimal, but if you want the maximum compression, just use 0.0 for the conpressionQuality. – picciano Jul 27 '15 at 23:50
  • Yes, I was trying a PNG instead of JPG to see if there was a difference in compression. – Paul Jul 27 '15 at 23:53
  • 1
    Just changing the extension will not change the filetype. It's still a jpg, but with the wrong extension. – picciano Jul 27 '15 at 23:54
  • UIImagePNGRepresentation is a separate method, but that format does not offer compression (on iOS). – picciano Jul 27 '15 at 23:54
  • Yeah, I was a bit confused if it did or not. – Paul Jul 27 '15 at 23:56
  • @picciano - PNG format is compressed. It doesn't offer as much compression as JPEG with low quality settings, but then again, It's a lossless compression. If you want compression without suffering JPEG artifacts, PNG is an excellent compromise. (It also supports transparencies, too.) – Rob Jul 28 '15 at 00:09
  • 1
    @rob yup, you're correct. I was oversimplifying. – picciano Jul 28 '15 at 00:17
  • @Paul - Is it important for your app to preserve the quality of the image, or is some quality loss acceptable? Also, are you OK losing any image meta data (e.g. capture date, type of camera, GPS, etc.) in this upload process? Bottom line, what are the functional needs of this app? BTW, if you're sending images that were captured on the device, sometimes going back and retrieving the original digital asset and avoid round-tripping it through a `UIImage` is best. – Rob Jul 28 '15 at 00:19

1 Answers1

0

Suggestions:

  1. Use the extension "jpg", not "png".
  2. Use 0.0 for the compressionQuality to maximize compression.
  3. Consider resizing the image smaller, if your situation allows.
picciano
  • 22,341
  • 9
  • 69
  • 82
  • There are several questions here on stack overflow that show you how to resize an image. If you can do this, it will likely give you the best results. See: http://stackoverflow.com/questions/2658738/the-simplest-way-to-resize-an-uiimage – picciano Jul 27 '15 at 23:56
  • Paul, you can follow these suggestions. For the 3rd step you can refer to this link http://stackoverflow.com/questions/2658738/the-simplest-way-to-resize-an-uiimage – Boopathy Jul 27 '15 at 23:57
  • If you need to resize an image for different sizes like large, medium, small , my opinion is to send it to the server and then resize. – Boopathy Jul 28 '15 at 00:06
  • Re point #2, I wouldn't generally advise using a compression quality that low unless you're OK with the image quality being visibly degraded in the process. Personally, I avoid going much less than 0.7 or 0.8. It depends upon the functional needs of the app, but if image fidelity is important, you might want to avoid lossy compression altogether. – Rob Jul 28 '15 at 00:13
  • 1
    @rob right again, depends on the purpose of the image. he was using 0.01, so not much difference, but yes, he needs to understand the trade off. – picciano Jul 28 '15 at 00:19
  • I put the quality at 0.01 to see if the photo uploaded to the server faster. More as a test. – Paul Jul 28 '15 at 00:29