MPMediaItemPropertyArtwork导致崩溃(奇怪的问题) [英] MPMediaItemPropertyArtwork causes crash (weird issue)
问题描述
运行额外循环"之前的分配
allocations before running 'extra 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 after running 'extra loop'
并进行澄清,在only-obj-c打开且系统库关闭的情况下调用堆栈(如果我打开它们,则最重的方法中最高的百分比是0.9%)
and to clarify, call stack with only-obj-c on and system libraries off (if i turn them on, the highest % is 0.9% per heaviest method)
我进行了一些研究,发现在 stackoverflow 中,这些VM:ImageIO_PNG_Data
通常是来自[UIImage imageNamed:]
,但是,正如您所看到的,我没有使用此方法,我只是从MPMediaItemCollection
获取引用.
I've made some research, and found at stackoverflow, that these VM:ImageIO_PNG_Data
are usually comming from [UIImage imageNamed:]
, however as you can see i don't use this method, i'm just getting the reference from MPMediaItemCollection
.
推荐答案
问题是UIImage通常只保留一个ref(CGImageRef),这是一个很小的值.显示项目后,CGImageRef被注入"了信息.结果,桌子一直在增长.
The problem was that UIImage usually keeps just a ref(CGImageRef), which is a small one. After displaying items, CGImageRef was 'injected' with information. As a result the table was growing all the time.
简单但不是最漂亮的解决方案是使用代码:
The simple but not the most beautiful solution was to use the code:
NSArray = @[obj1, obj2, obj3]; // where obj is custom NSObject and has a UIImage property
代替:
NSArray = @[img1, img2, img3]; // where img is of UIImage type
这篇关于MPMediaItemPropertyArtwork导致崩溃(奇怪的问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!