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

时间:2013-10-04 13:30:02

标签: ios objective-c memory-management instruments

在运行'额外循环'之前

分配 allocations before running 'crashing' loop

代码:

// 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.
}
运行'额外循环'后

分配 allocations before running 'crashing' loop

并澄清,调用堆栈只关闭-obj-c和系统库关闭(如果我打开它们,最高的%是每个最重的方法0.9%) call stack after extra loop

我做了一些研究,并在stackoverflow找到了这些VM:ImageIO_PNG_Data通常来自[UIImage imageNamed:],但是你可以看到我不使用这种方法,我我只是从MPMediaItemCollection获取参考资料。

2 个答案:

答案 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)的图像对象。我不太确定你在代码中想要实现的目标是什么?