gpt4 book ai didi

ios - "Message from debugger: Terminated due to memory issue "图片

转载 作者:行者123 更新时间:2023-11-28 14:11:42 25 4
gpt4 key购买 nike

我正在尝试从相册中获取图像,此代码从特定相册中获取所有图像,但在滚动应用程序时将关闭并给出错误“来自调试器的消息:由于内存问题而终止”。请检查代码并找到错误。(我想在没有内存警告的情况下获取所有相册和图像,如“Lalalab”应用程序)。

 func fatchImagesfromAlbum() {
DispatchQueue.global(qos: .background).async {
self.photoAssets = self.fetchResult as! PHFetchResult<AnyObject>

let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.image.rawValue)


self.photoAssets = PHAsset.fetchAssets(in: self.assetCollection, options: fetchOptions) as! PHFetchResult<AnyObject>

for i in 0..<self.photoAssets.count{
let asset = self.photoAssets.object(at: i)
let imageSize = CGSize(width: asset.pixelWidth,
height: asset.pixelHeight)

let options = PHImageRequestOptions()
options.deliveryMode = .fastFormat
options.isSynchronous = true

self.imageManager.requestImage(for: asset as! PHAsset, targetSize: imageSize, contentMode: .aspectFill, options: options, resultHandler: { (image, info) -> Void in


self.images.append(image!)
let url:NSURL = info!["PHImageFileURLKey"] as! NSURL
let urlString: String = url.path!
let theFileName = (urlString as NSString).lastPathComponent
print("file name\(info!)")
self.imageName.append("\(theFileName)")
self.imagePath.append("\(urlString)")

})

print(self.imagePath)
print(self.imageName)
DispatchQueue.main.async
{
[unowned self] in

self.collectionView.reloadData()
}


}
}
PHPhotoLibrary.shared().register(self)

if fetchResult == nil {
let allPhotosOptions = PHFetchOptions()
allPhotosOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
fetchResult = PHAsset.fetchAssets(with: allPhotosOptions)
}


}

最佳答案

遇到这样的问题;它与制作 weakunowned 引用无关。当您的 Objective-C 代码或使用 Cocoa 类创建对象时,您应该做的是处理 autoreleasepool ,尝试在 autoreleasepool 中调用您的方法:

autoreleasepool {
fatchImagesfromAlbum()
}

应该是 fetchImagesfromAlbum 而不是 fatchImagesfromAlbum 吗?

引自 Advanced Memory Management Programming Guide :

Autorelease pool blocks provide a mechanism whereby you can relinquish ownership of an object, but avoid the possibility of it being deallocated immediately (such as when you return an object from a method). Typically, you don’t need to create your own autorelease pool blocks, but there are some situations in which either you must or it is beneficial to do so.

但是,应该不需要autoreleasepool中执行整个方法,可能内存问题的原因是迭代地执行 fetchAssetsrequestImage(在 for 循环内)。


引用:Is it necessary to use autoreleasepool in a Swift program?

关于ios - "Message from debugger: Terminated due to memory issue "图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52473796/

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