【问题标题】:autorelease with creating UIImage in ImageContext在 ImageContext 中创建 UIImage 的自动释放
【发布时间】:2015-02-09 17:11:29
【问题描述】:

我正在绘制到自动释放池内的 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 ,它们是否在正确的位置。 我希望能澄清这一点。 谢谢

雷扎

【问题讨论】:

  • 请发布您的崩溃日志,谢谢。
  • 这是很少发生的情况之一,实际上只有一次,并且由于我在开发方案中启用了僵尸,它只是在调用中停止了。这就是为什么我不确定是电话还是其他原因。由于我启用了僵尸,我希望如果它是一个错误,它应该总是在调用中停止,但它不会

标签: ios objective-c uiimage


【解决方案1】:

return 移出自动释放池,以确保您的图像不会被丢弃。执行以下操作之一:

ARC

UIImage *image = nil;
@autoreleasepool {
    // ...
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}
return image;

非 ARC

UIImage *image = nil;
@autoreleasepool {
    // ...
    image = [UIGraphicsGetImageFromCurrentImageContext() retain];
    UIGraphicsEndImageContext();
}
return [image autorelease];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-09
    相关资源
    最近更新 更多