内存泄漏来自Swift中的ALAssetRepresentation CGImage

时间:2014-12-23 05:46:09

标签: ios memory-leaks alasset

我正在UITableView中从资源加载图像,我注意到我从defaultAssetRepresentation.fullResolutionImage.CGImage.takeunretainedValue加载了与我加载的CGImage相关的内存泄漏。

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(kReviewDisplayCell, forIndexPath: indexPath) as ReviewDisplayCollectionViewCell
    let asset = assets.objectAtIndex(indexPath.row) as ALAsset
    var cgImage = asset.defaultRepresentation().fullResolutionImage().takeUnretainedValue()
    var orientation:UIImageOrientation?
    var orientationValue:NSNumber = asset.valueForProperty(ALAssetPropertyOrientation) as NSNumber
    orientation  = self.correctOrientation(orientationValue)
    var image = UIImage(CGImage: cgImage, scale: 0.5, orientation: orientation!)
    cell.imageView.image = image

    return cell
}

根据记忆泄漏仪器,泄漏似乎与: NSPathStore2对象。 负责任的框架:+[NSPathStore2 pathStoreWithCharacters:Length:]

上面的函数在堆栈的早期调用: -[ALAssetRepresentation _fileDescriptor] 然后 -[PLGateKeeperClient fileDescriptorForAssetURL:]

我很抱歉我没有足够的声誉来发布我的屏幕截图。

在分配工具上,即使在导航控件解除了视图控制器之后,也总是从UITableViewController中保留CGImage,我无法弄清楚是否有来自某处的强引用。有没有办法在使用ARC时手动释放CGImage参考?我尝试将上面的代码放在autoreleasepoool {}中,但它没有用。提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

对于那些仍未找到所需内容并迷惑了这个问题的人,使用CGImage时需要使用自动释放功能进行包装,可能类似于:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(kReviewDisplayCell, forIndexPath: indexPath) as ReviewDisplayCollectionViewCell
    let asset = assets.objectAtIndex(indexPath.row) as ALAsset
    autorelease {
        var cgImage = asset.defaultRepresentation().fullResolutionImage().takeUnretainedValue()
        var orientation:UIImageOrientation?
        var orientationValue:NSNumber = asset.valueForProperty(ALAssetPropertyOrientation) as NSNumber
        orientation  = self.correctOrientation(orientationValue)
        var image = UIImage(CGImage: cgImage, scale: 0.5, orientation: orientation!)
        cell.imageView.image = image
    }   
    return cell
}

请参见this answer for details