ios - 在 ImageContext 中创建 UIImage 的自动释放

标签 ios objective-c uiimage

我正在绘制到自动释放池内的 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/

相关文章:

ios - 如何在没有奇怪动画的情况下关闭 2 个模态视图 Controller

ios - 从 appdelegate 展示一个 View Controller

iphone - Tableview 中没有行的节数

objective-c - 在类型错误的对象上找不到属性,但属性存在

ios - 在纵横比中设置图像高度在 Xcode 中填充自动布局

ios - 使用给定路径剪切图像

Swift:刷新 UIImageView 图像

ios - 具有复杂触摸代码的 View 是否应该有自己的 View Controller ?

ios - Cordova 3.3 - fileSystem.root.fullPath 返回 "/"而不是完整路径

objective-c - 在 NSImage 中处理多页图像