【问题标题】:UIIMageView, warning: check_safe_call: could not restore current frameUIIMageView,警告:check_safe_call:无法恢复当前帧
【发布时间】:2011-02-27 04:51:22
【问题描述】:

我正在根据加速度计输入更改 UIImageView 中的图像。图像存储在数组中。 应用程序可以正常运行一段时间,然后崩溃。

警告:check_safe_call:无法恢复当前帧

  1. 我在填充数组时没有使用“UIImage ImageNamed”方法。
  2. 所有图像的总大小约为 12 Mb。但是单个图像非常轻(
  3. 以防万一有任何自动释放,我已经分配了一个 NSAutoreleasePool 视图确实加载并在 didReceiveMemoryWarning 方法中耗尽它(在应用程序崩溃之前确实被调用了 2、3 次?)。

以下是创建图像数组的代码:

    //create autorelease pool as we are creating many autoreleased objects
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSMutableArray *finalarr = [[NSMutableArray alloc] initWithCapacity:9];

NSLog(@"start loading");
for(int y = 0; y < 100; y+=10)
{
    NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:10];
    for(int x = 0; x < 10; x++)
    {
        NSString *imageName = [[NSString alloc] initWithFormat:@"0%d",y + x];

        UIImage *img = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"png"]];
        [arr addObject:img];

        [imageName release];
        [img release];
    }
    [finalarr addObject:arr];
    [arr release];

}

NSLog(@"done loading");
// Override point for customization after app launch
viewController.imagesArray = finalarr;
[finalarr release];
//retain the array of images
[viewController.imagesArray retain];

//release the aurtorelease pool to free memory taken up by objects enqued for release
[pool release];

由于应用程序运行平稳一段时间,这意味着创建数组绝对不是问题。

在此之后,从 [accelerometer didAccelerate] 调用以下方法

-(BOOL)setImageForX:(int)x andY:(int)y
{
[self.imageView setImage:(UIImage*)[(NSArray*)[self.imagesArray objectAtIndex:y] objectAtIndex:x]];
return TRUE;
}

所以唯一被执行的代码是“UIImageView setImage”。但是这里没有创建对象。

如果代码似乎有任何泄漏或者我做错了什么,请告诉我。

谢谢,
交换无

【问题讨论】:

    标签: iphone memory memory-management memory-leaks uiimageview


    【解决方案1】:

    NSAutoreleasePool应该以同样的方式分配和释放。在一个地方分配它并在另一个地方释放它通常非常糟糕(但是您粘贴的示例代码正确使用它)

    将图像加载到内存中解压缩它们;一个 100KB 的压缩 PNG 很容易解压到 2MB。

    您似乎还有一个额外的 retain 调用,不应该存在:[viewController.imagesArray retain];(我假设 imagesArray 属性标记为 copyretain 而不是 assign

    【讨论】:

    • 哇!感谢您提供图像解压缩提示。我不知道这个。
    • 另外,关于额外的保留调用,您是对的。删除它!
    猜你喜欢
    • 2012-11-28
    • 2012-03-27
    • 1970-01-01
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多