Closed macinsmith closed 11 years ago
The purpose of the lines you're removing is to retain the image until the end of the run loop. That's accomplished by creating an autoreleased UIImage just to own the Core Graphics image until then. It's an ugly hack.
If you remove those lines, then you're leaking an image every time this method is called, and your app will eventually be killed for hogging memory.
Is the crash occurring in a clean clone of Leaves, or just in your modified version?
Closing due to inactivity.
I modified a version of this to use with an app on the iPad. It would work in the simulator, but crashed on the iPad. I found that the problem was that the CGImageRef was being released, then passed back in - (CGImageRef) imageForPageIndex:(NSUInteger)pageIndex. Also there was a call to [UIImage imageWithCGImage:] that was not needed. Here is what my code now looks like
(CGImageRef) imageForPageIndex:(NSUInteger)pageIndex { CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(NULL, pageSize.width, pageSize.height, 8, /* bits per component/ pageSize.width * 4, / bytes per row */ colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); CGColorSpaceRelease(colorSpace); if (context == nil) { //NSLog(@"Failed to create context for pageIndex=%d", pageIndex); return nil; } CGContextClipToRect(context, CGRectMake(0, 0, pageSize.width, pageSize.height)); [dataSource renderPageAtIndex:pageIndex inContext:context]; CGImageRef image = CGBitmapContextCreateImage(context); CGContextRelease(context);
// REMOVED THE FOLLOWING TWO LINES // [UIImage imageWithCGImage:image]; // ?? serves no purpose // CGImageRelease(image); // releases image then passes it back to caller
return image; }