Any suggestions on how to handle this crash in CGImageDestinationFinalize?

依然范特西╮ 提交于 2019-12-03 09:15:28

I created a test project from your description which loads and saves the image from the provided URL. It runs without problems in the simulator and on an iPhone 4 (iOS 4.3.2).

Could you try to run the following method in your project/environment:

- (void)checkImage
{
    NSURL *imageLocationOnDisk = [[NSBundle mainBundle] URLForResource:@"htg-logo-tiny"
                                                         withExtension:@"gif"];
    assert(imageLocationOnDisk);

    CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)imageLocationOnDisk, NULL);
    assert(imageSource);

    id options = [NSDictionary dictionaryWithObjectsAndKeys:
                  (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailWithTransform,
                  (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailFromImageIfAbsent,
                  [NSNumber numberWithInt:1024], kCGImageSourceThumbnailMaxPixelSize,
                  (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailFromImageAlways,
                  nil];

    CGImageRef image = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, (CFDictionaryRef)options);
    assert(image);

    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* file = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"foo.png"];

    options = [NSDictionary dictionaryWithObjectsAndKeys:
               [NSNumber numberWithInt:1], (id)kCGImagePropertyOrientation,
               (id)kCFBooleanFalse, (id)kCGImagePropertyHasAlpha,
               [NSNumber numberWithInt:1.0], kCGImageDestinationLossyCompressionQuality,
               nil];
    CGImageDestinationRef dest = CGImageDestinationCreateWithURL((CFURLRef)[NSURL fileURLWithPath:file],
                                                                 kUTTypePNG,
                                                                 1,
                                                                 NULL);
    assert(dest);
    CGImageDestinationAddImage(dest, image, (CFDictionaryRef)options);
    CGImageDestinationFinalize(dest);
}

To run the test you have to copy the image to your project's resources.

As I said above code works fine on my machine. I also tried it successfully with CGImageSourceCreateImageAtIndex.

The fact that the error happens in your app might be related to some other kind of error. Perhaps there's an issue with your threaded use of the ImageIO API.

One thing I noticed was the options argument you passed in the CGImageDestinationCreateWithURL call. According to the documentation it should be NULL.

Edit: The image in question has a size of 792 × 1111 (as reported by both PhotoShop and Preview.app) and is mostly black.

What you can do is storing the image in a UIImage object, drawing it in a new context (using the size property) and then making a CGImageRef object of it. This will fix the corruption. Though you need to do this for every image you receive and this will be a small performance penalty.

Note that you must send messages to UIImage from the main thread.

I'm gonna publish the soution of similar crash I found after hour of debugging, maybe it would be useful for somebody... The reason was in so stupid thing. I had two placeholders in NSLog and only one real variable.

NSLog(@"lastItemDate = %@ unixtime = %@", lastItemDate);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!