gpt4 book ai didi

ios - Swift 中 ALAssetRepresentation CGImage 的内存泄漏

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:37:25 27 4
gpt4 key购买 nike

我正在从我的 UITableView 中的 Assets 加载图像,我注意到我遇到了与从 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:]

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

在分配工具上,即使导航控件关闭了 View Controller ,也总是从 UITableViewController 保留 CGImage,我无法确定是否从某处强烈引用它。有没有办法在使用 ARC 时手动释放 CGImage 引用?我尝试将上面的代码放在 autoreleasepoool{} 中,但它没有用。预先感谢您的帮助。

最佳答案

对于那些仍然没有找到他们要找的东西,并且偶然发现这个问题的人,使用 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
autoreleasepool {
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 .

关于ios - Swift 中 ALAssetRepresentation CGImage 的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27614746/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com