我正在绘制到自动释放池内的 ImageContext 以控制我的内存占用,并从自动释放池内部返回图像。我在这个调用中发生了崩溃,我想知道它是否是由于调用函数使用之前在自动释放池中释放的图像引起的。这是我的代码:
-( UIImage *) imageRepFromShapes
{
CGRect sheetDisplayView = [ UIScreen mainScreen ].bounds;
@autoreleasepool {
UIGraphicsBeginImageContextWithOptions( boundingRect_m.size, NO, 0.0 );
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);
for ( Shape *shape in arryOfShapes ) {
[ shape drawShape ];
}
UIGraphicsPopContext();
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
}
另一个问题是我在这个调用中是否需要 UIGraphicsPushContext 和 UIGraphicsPopContext ,它们是否在正确的位置。
我希望能澄清这一点。
感谢
礼萨
最佳答案
移动return
在自动释放池之外,以确保您的图像不会被丢弃。执行以下操作之一:
弧
UIImage *image = nil;
@autoreleasepool {
// ...
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return image;
非 ARC
UIImage *image = nil;
@autoreleasepool {
// ...
image = [UIGraphicsGetImageFromCurrentImageContext() retain];
UIGraphicsEndImageContext();
}
return [image autorelease];
关于ios - 在 ImageContext 中创建 UIImage 的自动释放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28415342/