[英]Images not release in memory
我有帶有圖像單元格的桌子。 圖像從Internet下載並保存在本地磁盤中。 計數=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];
為什么隱藏圖像不釋放?
替換此行
UIImage* image = [[UIImage alloc] initWithContentsOfFile:imagePath];
與此並檢查一次
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
好吧,我使用了一個自定義MSAsyncImageCell
類,該類在被iOS重用時會替換其圖像。 它異步地從URL加載圖像,優雅地顯示“正在加載”圖像,並在用於其他圖像時釋放內存。 如果您願意,我可以在此處發布代碼,但是代碼有點長。
這是實際加載圖像的代碼部分。 我的圖片不在網絡/緩存中,所以是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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.