分配
代码:
// loading items to the array, there are no memory warnings after this is completed. The first allocations screenshot is after this code and before extra loop code.
NSMutableArray *albumCovers = [[NSMutableArray alloc] init];
for (MPMediaQuery *query in queries) {
NSArray *allCollections = [query collections];
for (MPMediaItemCollection *collection in allCollections) {
MPMediaItemArtwork *value = [collection.representativeItem valueForProperty:MPMediaItemPropertyArtwork];
UIImage *image = [value imageWithSize:CGSizeMake(100, 100)];
if (image) {
[albumCovers addObject:image];
}
}
}
}
_mediaCollections = [NSArray arrayWithArray:artworkedCollections];
_albumCovers = [NSArray arrayWithArray:albumCovers];
}
以及其他地方:
// !!!!! extra loop - from here the memory starts to grow and never release
for (i=0; i< 800; i++) {
UIImage * coverImage = [_albumCovers objectAtIndex:indexPath.row];
[veryTemp setImage:coverImage]; // exactly this line adds to the memory. with this line commented, there is no problem.
}
运行'额外循环'后分配
并澄清,调用堆栈只关闭-obj-c和系统库关闭(如果我打开它们,最高的%是每个最重的方法0.9%)
我做了一些研究,并在stackoverflow找到了这些VM:ImageIO_PNG_Data
通常来自[UIImage imageNamed:]
,但是你可以看到我不使用这种方法,我我只是从MPMediaItemCollection
获取参考资料。
答案 0 :(得分:1)
问题是UIImage通常只保留一个ref(CGImageRef),这是一个小的。显示项目后,CGImageRef被“注入”了信息。结果桌子一直在增长。
简单但不是最美丽的解决方案是使用代码:
NSArray = @[obj1, obj2, obj3]; // where obj is custom NSObject and has a UIImage property
而不是:
NSArray = @[img1, img2, img3]; // where img is of UIImage type
答案 1 :(得分:0)
换入@autorelease
游泳池?
另外,为什么要在UIImageView
中为veryTemp设置图像(我假设它是[veryTemp setImage:coverImage];
)800次?
最后:
[_albumCovers objectAtIndex:indexPath.row];
您在循环中获取完全相同的索引(indexPath.row
)的图像对象。我不太确定你在代码中想要实现的目标是什么?