imageWithCGImage and memory

前端 未结 8 402
既然无缘
既然无缘 2020-12-10 02:24

If I use [UIImage imageWithCGImage:], passing in a CGImageRef, do I then release the CGImageRef or does UIImage take care of this itse

相关标签:
8条回答
  • 2020-12-10 02:50

    The ownership in UIImage about CGImage is unclear. It seems like don't copy the CGImage but it's not guaranteed by documentation. So we have to handle that ourselves.

    I used a new subclass of UIImage to handle this problem. Just retaining passing CGImage, and releasing it when the dealloc.

    Here is sample.

    @interface  EonilImage : UIImage
    {
        @private
        CGImageRef      sourceImage;
    }
    - (id)initWithCGImage:(CGImageRef)imageRef scale:(CGFloat)scale orientation:(UIImageOrientation)orientation;
    
    @end
    
    
    
    @implementation     EonilImage
    
    - (id)initWithCGImage:(CGImageRef)imageRef scale:(CGFloat)scale orientation:(UIImageOrientation)orientation
    {
        self    =   [super initWithCGImage:imageRef scale:scale orientation:orientation];
    
        if (self) 
        {
            sourceImage =   imageRef;
            CGImageRetain(imageRef);
        }
    
        return  self;
    }
    - (void)dealloc
    {
        CGImageRelease(sourceImage);
        [super dealloc];
    }
    @end
    

    Because the CGImage returned by -[UIImage CGImage] property is not guaranteed to be same CGImage passed into init method, the class stores CGImage separately.

    0 讨论(0)
  • 2020-12-10 02:50

    Not sure if this helps, but I had a similar problem. I read the answers and then did the following which appears to have fixed it:

    CGImageRef cgImage = [asset thumbnail];
        UIImage *thumbImage = [[UIImage imageWithCGImage:cgImage ]retain];
        UIImageView *thumbImageView = [[UIImageView alloc] initWithImage:thumbImage];
        CGImageRelease(cgImage);
    

    I used the autorelease as suggested but it was not enough. One I had added the image to the UIImageView I then released the cgImage.
    It didn't crash and deallocated nicely. Why -- I have no idea but it worked.

    The asset thumbnail part is from the ALAsset Library by the way, you might need something else there.

    0 讨论(0)
  • 2020-12-10 02:51

    <> Not my opinion. I had the same problem. According to documentation

    UIImage *image = [[UIImage alloc] initWithCGImage:imageRef];
    

    imho keeps all references correct. If you are using a CGDataProvider please have a look at CGDataProviderReleaseDataCallback and set a breakpoint in your callback. You can see that is is correctly called after you [release] your image and you can free() your image data buffer there.

    0 讨论(0)
  • 2020-12-10 02:56

    I agree with you -- the documentation is muddled at best for this API. Based on your experiences, then, I would conclude that you are responsible for the lifetime of both objects - the UIImage and the CGImageRef. On top of that you have to make sure the lifetime of the CGImageRef is at least as long as the UIImage (for obvious reasons).

    0 讨论(0)
  • 2020-12-10 02:56

    According to the fundamental rule of Cocoa memory management, an owning object should release the owned object when it no longer needs it. Other objects are responsible for taking and releasing ownership on their own. If a UIImage needs an object to persist but doesn't retain or copy it, it's a bug in UIImage's implementation and should be reported as such.

    0 讨论(0)
  • 2020-12-10 03:07

    Are you using Xcode 3.1 on Snow Leopard? I am experiencing the same issue:

    CGContextRef ctx = ...;
    CGImageRef cgImage = CGBitmapContextCreateImage(ctx);
    UIImage * newImage = [[UIImage imageWithCGImage:cgImage] retain];
    
    CGImageRelease(cgImage); // this should be OK, but it now crashes in Simulator on SL
    

    I'm guessing that upgrading to Xcode 3.2 will fix the issue.

    0 讨论(0)
提交回复
热议问题