CGImage
to a UIImage
and add it to a UIImageView
on the main thread.UICollectionViewCell
from alpha value 0 to 1.UICollectionView
The behavior, at first, acts normally, but quickly digressed into unpredictableness, and usually and quickly resulted in EXC_BAD_ACCESS
errors, happening somewhere in the process of converting uiimage to cgimage or vice versa.
//setup for background drawing
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGContextRef context;
context = CGBitmapContextCreate(NULL, 250, backgroundHeight - 112, 8, 250 * 4, colorSpaceRef, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
CGColorSpaceRelease(colorSpaceRef);
//Take the image I want drawn, convert to cgimage for background drawing
CGImageRef image = contentImage.CGImage;
CGContextDrawImage(context, CGRectMake(0, 0, 250, backgroundHeight - 112), image);
CGImageRelease(image);
CGImageRef outputImage = CGBitmapContextCreateImage(context);
imageRef = outputImage;
[self performSelectorOnMainThread:@selector(addImageToView) withObject:nil waitUntilDone:YES];
CGContextRelease(context);
CGImageRelease(outputImage);
The addImageToView Method simply create an image and adds to my UIImageView
UIImage * image = [UIImage imageWithCGImage:imageRef];
[photoView setImage:image];
These methods get called during the cellForRowAtIndexPath method, along with my method fadeInImage
[UIView animateWithDuration:.6 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
photoView.alpha = 1;
}completion:nil];
When I run, I get the Bad Access calls and crashing. I'm guessing it has something to do with the main thread and the background threads passing the images between one another. Any ideas? Thanks guys.
I think, as long as you didnt create an image with CGImageCreate
or used CGImageRetain
, you dont have to use CGImageRelease(image);
. It should release automatically once you stop using it. Check it out.