MPMediaItemPropertyArtwork导致崩溃(奇怪的问题)

运行’额外循环’之前的分配 在运行'崩溃'循环之前进行分配

代码:

// 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获取引用。

问题是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 

包装在@autorelease池中?

另外,为什么你在[veryTemp setImage:coverImage];为veryTemp设置图像(我假设它是一个UIImageView )800次[veryTemp setImage:coverImage];

最后:

 [_albumCovers objectAtIndex:indexPath.row]; 

您将在循环中获取完全相同的索引( indexPath.row )的图像对象。 我不太确定你在代码中想要实现的目标是什么?