UIImageJPEGRepresentation bug?

I've been using UIImageJPEGRepresentation to take a cropped screen shot and share it using the Copy function on iOS.

Sometimes the image that is given to Copy by my App is clipped.

Only the bottom 1/3 to 1/4 of the image makes it through UIImageJPEGRepresentation.

I tried lowering the compression from 0.7 to 0.5 which seemed to fix it for the reproducable screen condition I had found.

Now I find that it's happening again I've changed the compression to 1.0, effectively turning it off (and the bug, I'm hoping).

It appears to work for all the cases that I can reproduce so I can only conclude it's a bug in this routine.


- (void) shareImageByCopy : (UIImage *) image withTitle : (NSString *) title {
    NSData *compressedImage = UIImageJPEGRepresentation(image, 1.0);
    NSString *imagePath = [[FileUtils applicationTemporaryDirectory] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg",title]];
    NSURL *imageUrl = [NSURL fileURLWithPath:imagePath];
    [compressedImage writeToURL:imageUrl atomically:YES];
    NSArray *activityItems = @[title,imageUrl];
    UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems
                                                                                     applicationActivities:nil];

Anyone else experiencing this? I updated to iOS 11.4.1 yesterday so it's still current at time of writing this.

Replies

Any reason you are not using UIImagePNGRepresentation() instead? Graphics like screenshots are better in PNG format, and photographs are better in JPEG format.