简体   繁体   中英

UIImage from CGImageRef

I am trying a simple test for a much more complex project but I am baffled as to why the code below is crashing and giving an EXC_BAD_ACCESS error?

This is called from a UIView.

 - (void)testing {
      NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"ball.png" ofType:nil];
      CGImageRef imageRef = [[[UIImage alloc]initWithContentsOfFile:imagePath]CGImage];
 //   CGImageRetain(imageRef);

      UIImage *newImage = [[UIImage alloc]initWithCGImage:imageRef];
      UIImageView *iv = [[UIImageView alloc]initWithImage:newImage];
      [self addSubview:iv];
 }

My guess is that the CGImageRef is not being retained but adding CGImageRetain(imageRef); makes no difference.

I should also note that this project has ARC turned on.

EDIT

I did a little bit more testing and have discovered that this is directly related to ARC as I created 2 basic projects including only the code above. The first with ARC turned off and it worked perfectly. The next with ARC turned on and BAM crash with the same error. The interesting thing is that I got an actual log error ONLY the first time I ran the project before the crash.

 Error: ImageIO: ImageProviderCopyImageBlockSetCallback 'ImageProviderCopyImageBlockSetCallback' header is not a CFDictionary...

This line is the problem:

CGImageRef imageRef = [[[UIImage alloc]initWithContentsOfFile:imagePath]CGImage];

The created UIImage will be released immediately following this full-expression (eg after this line). So even trying to add a CGImageRetain() afterwards won't work.

The fundamental problem is the CGImageRef returned from CGImage is almost certainly an ivar of the UIImage and will be released when the UIImage is deallocted.

The generic way to fix this is to extend the lifetime of the UIImage . You can do this by placing the UIImage into a local variable and referencing it after your last reference to the CGImage (eg with (void)uiimageVar ). Alternatively, you can retain the CGImageRef on that same line, as in

CGImageRef imageRef = CGImageRetain([[[UIImage alloc] initWithContentsOfFile:imagePath] CGImage]);

But if you do this, don't forget to release the imageRef when you're done.

You wrote this:

CGImageRef imageRef = [[[UIImage alloc]initWithContentsOfFile:imagePath]CGImage];

That line creates a UIImage , gets its CGImage property, and then releases the UIImage object. Since you're not retaining the CGImage on that line, the only owner of the CGImage is the UIImage . So when the UIImage is released and immediately deallocated, it deallocates the CGImage too.

You need to retain the CGImage before the UIImage is released. Try this:

CGImageRef imageRef = CGImageRetain([[UIImage alloc]initWithContentsOfFile:imagePath].CGImage);

and then at the end of the function:

CGImageRelease(imageRef);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM