I am confused why my app is crashing with this error.
I have implemented the displayLayer method (for rendering CALayer).
The first time this method runs things work fine. But subsequent calls to this is when the error occurs.
The error seems to occur when the self.bgColor is being set to the context fill color.
INTERESTINGLY… if I create the bgColor right before that line, things work. But as it stands, the bgColor is being created upon initialization of this class (the container of the displayLayer method).
-(void)displayLayer:(CALayer *)caLayer
{
UIGraphicsBeginImageContext(caLayer.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, self.bgColor);
CGContextFillRect(context, CGRectMake(0, 0, 320, 25));
[self drawText:context];
// get image buffer
UIImage *imageBuffer = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// set layer contents to image buffer
caLayer.contents = (id)[imageBuffer CGImage];
}
I haven’t done much iPhone programming yet, and never used a CGColor instance variable, so what I would do is this:
[UIColor blackColor] returns an autoreleased object, and you assigned it to your instance variable without retaining it.
Using self.bgColor instead of just bgColor in init and having set up the property to retain its value will make sure that the color is retained and can be used in displayLayer later on.
As I mentioned I don’t have any experience with using CGColors directly, that’s why I’m using a UIColor in the above code. Please adjust as needed.