【发布时间】:2011-06-01 20:01:19
【问题描述】:
我在使用这个返回 CGImageRef 的自定义方法时发生了内存泄漏。我无法正确释放“cgImage”,因为我必须返回它。我该怎么办?
- (CGImageRef)rectRoundedImageRef:(CGRect)rect radius:(int)radius
{
CGSize contextSize = CGSizeMake(rect.size.width, rect.size.height);
CGFloat imageScale = (CGFloat)1.0;
CGFloat width = contextSize.width;
CGFloat height = contextSize.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, width * imageScale, height * imageScale, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
// Draw ...
// Get your image
CGImageRef cgImage = CGBitmapContextCreateImage(context);
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
//CGImageRelease(cgImage); //If I release cgImage the app crashes.
return cgImage;
}
【问题讨论】:
-
我也有类似的泄漏。在这里试试我的答案:stackoverflow.com/a/23669476/3631310
标签: objective-c memory-management quartz-graphics