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

标签 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

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

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

最佳答案

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

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

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/

相关文章:

ios - 将简单物理应用于 SceneKit XCODE SWIFT 中的 .scn 对象

ios - NSUserDefaults nullcheck 不起作用

iphone - iOS 7 后退按钮弹出手势

delphi - 如何释放接口(interface)对象 (Delphi 7)

linux - 分配内存给其他进程不能在linux中使用的进程

objective-c - iOS ivar alloc/init 分配内存地址给另一个ivar

ios - 如何创建类的单个实例并在多个 View 中更新该类的数据?

ios - 在运行时动态下载和执行 iOS 代码

ios - 应用程序在模拟器中完美运行,但在 iPod Touch 上运行时出现 "Undefined Symbols"错误

ios - Spritekit bodyWithbodies 不起作用