2

运行“额外循环”之前的分配 运行“崩溃”循环之前的分配

编码:

// 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.
}

运行“额外循环”后的分配 运行“崩溃”循环之前的分配

并澄清一下,在 only-obj-c 和系统库关闭的情况下调用堆栈(如果我打开它们,最高百分比是每个最重方法的 0.9%) 额外循环后调用堆栈

我进行了一些研究,并在stackoverflow发现这些VM:ImageIO_PNG_Data通常来自[UIImage imageNamed:],但是如您所见,我不使用此方法,我只是从MPMediaItemCollection.

4

2 回答 2

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
于 2013-11-21T09:25:25.897 回答
0

包在@autorelease游泳池里?

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

最后:

[_albumCovers objectAtIndex:indexPath.row];

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

于 2013-10-04T13:53:19.897 回答