OGeek|极客世界-中国程序员成长平台

标题: ios - MPMediaItemPropertyArtwork 导致崩溃(奇怪的问题) [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:18
标题: ios - MPMediaItemPropertyArtwork 导致崩溃(奇怪的问题)

运行“额外循环”之前的分配 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

澄清一下,在 only-obj-c 开启和系统库关闭的情况下调用堆栈(如果我打开它们,每个最重方法的最高 % 为 0.9%) call stack after extra loop

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



Best Answer-推荐答案


问题是 UIImage 通常只保留一个 ref(CGImageRef),它很小。显示项目后,CGImageRef 被“注入(inject)”了信息。结果,这张 table 一直在增长。

简单但不是最漂亮的解决方案是使用代码:

NSArray = @[obj1, obj2, obj3]; // where obj is custom NSObject and has a UIImage property

代替:

NSArray = @[img1, img2, img3]; // where img is of UIImage type

关于ios - MPMediaItemPropertyArtwork 导致崩溃(奇怪的问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19182495/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4