我的应用程序由于在应用程序使用过程中的某个时间点出现内存压力而终止,我已将问题隔离为导致问题的一大块代码。
我将复制下面的代码(我通过注释掉这段代码并运行应用程序进行了测试,应用程序运行良好)。
我在这里要做的是:我正在阅读 RSS 提要,获取图像的 URL 并将其显示在 tableView 单元格中。下面的代码来自 cellForRowAtIndexPath。
// 1. Check the image cache to see if the image already exists. If so, then use it. If not, then download it.
if ([[ImageCache sharedImageCache] DoesExist:imgSrcString] == true)
{
cell.cellImageView.image = [[ImageCache sharedImageCache] GetImage:imgSrcString];
} else {
cell.cellImageView.image = [UIImage imageNamed:@"NoImage_Small"];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
// Now, we can’t cancel a block once it begins, so we’ll use associated objects and compare
// index paths to see if we should continue once we have a resized image.
objc_setAssociatedObject(cell,
kIndexPathAssociationKey,
indexPath,
OBJC_ASSOCIATION_RETAIN);
dispatch_async(queue, ^{
NSData * imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgSrcString]];
UIImage *image = [UIImage imageWithData:imageData];
//UIImage *resizedImage = [image scaleToSize:CGSizeMake(71.0f,71.0f)] ; //[UIImage resizeImage];
dispatch_async(dispatch_get_main_queue(), ^{
NSIndexPath *cellIndexPath =
(NSIndexPath *)objc_getAssociatedObject(cell, kIndexPathAssociationKey);
if(image){
if ([indexPath isEqual:cellIndexPath]) {
[[cell cellImageView] setImage:image];
[[ImageCache sharedImageCache] AddImage:imgSrcString :image];
}
}
// [[ImageCache sharedImageCache] AddImage:[imgSrcString stringByAppendingString:@"bigImage"] :image];
});
});
}
任何帮助我指出正确方向的帮助将不胜感激。谢谢你的时间 !