图像无法在内存中释放

时间:2013-10-16 11:12:50

标签: ios image memory-warning

我有图像单元格的表格。图像从互联网下载并保存在本地磁盘中。计数= 200.Tableview显示此图像。将内容滚动到底部时,会显示消息内存警告...已用内存250 - 300 mb O_O !!!链接到不保留的图像。

NSString *cellID = @"cellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
...
NSString* imagePath = [arrayContent objectAtIndex:indexPath.row];
UIImage* image = [[UIImage alloc] initWithContentsOfFile:imagePath];
[cell.imageView setImage:image];

为什么隐藏图片不能发布?

2 个答案:

答案 0 :(得分:6)

替换此行

UIImage* image = [[UIImage alloc] initWithContentsOfFile:imagePath];

用这个并检查一次

 UIImage *image = [UIImage imageWithContentsOfFile:imagePath];

答案 1 :(得分:0)

我使用了一个自定义MSAsyncImageCell类,在iOS重用时替换了它的图像。它以异步方式从URL加载图像,正常显示“加载”图像,并在重用另一个图像时释放内存。如果你愿意,我可以在这里发布代码,但代码有点长。

以下是实际加载图像的代码部分。我有关于web /缓存的图像所以它是NSData。

- (BOOL)_loadImageWithData:(NSData *)imageData
{
    UIImage *image = [UIImage imageWithData:imageData];

    // Just in case loading failed.

    if (image)
    {
        // Extra check - don't mix up.
        if ([[self.currentURL absoluteString] isEqualToString:[self.imageURL absoluteString]])
            self.asyncImageView.image = image;

        return YES;
    }

    return NO;
}