【问题标题】:MPMediaItemPropertyArtwork causes crash (weird issue)MPMediaItemPropertyArtwork 导致崩溃(奇怪的问题)
【发布时间】:2013-10-11 12:38:52
【问题描述】:

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

代码:

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

【问题讨论】:

    标签: ios objective-c memory-management instruments


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      包裹在@autorelease池中?

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

      最后:

      [_albumCovers objectAtIndex:indexPath.row];
      

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

      【讨论】:

      • 1.我将它添加到 UIImageView 800 次仅用于测试目的。通常图像在集合视图中使用,但我想确保这是图像问题而不是集合视图之一。 2. IndexPath.row 正在改变,因为它是更大代码的一部分。 “额外循环”是与上面的代码不同的方法;) 3. Autorelease 已经过测试并且没有帮助。
      猜你喜欢
      • 2011-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-14
      • 2013-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多