gpt4 book ai didi

ios - 在内存不足的情况下检索图像

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

我正在尝试从用户的相机胶卷中检索图像,但遇到内存不足的问题。我一次加载 50 张图像,但不幸的是,加载几次后它会耗尽内存并崩溃。我试过将整个语句包装在一个自动释放池中,但事实证明这没有效果。这是我的代码...

func retrieveImages(thumbnail: Bool, indexOfLibrary: Int) {
/* Retrieve the items in order of modification date, ascending */
var indexSet: NSIndexSet!

let options = PHFetchOptions()

options.sortDescriptors = [NSSortDescriptor(key: "creationDate",
ascending: false)]

/* Then get an object of type PHFetchResult that will contain
all our image assets */
let assetResults = PHAsset.fetchAssetsWithMediaType(.Image,
options: options)

if totalPostsCounted == false {
assetResults.enumerateObjectsUsingBlock { (object: AnyObject!, count: Int, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
if object is PHAsset {
self.totalPosts = count
} else {
self.totalPostsCounted = true
}
}
}

if thumbnail == true {
if totalPosts - libraryImageThumbnails.count < 50 {
allImagesLoaded = true
} else {
if firstTimeLoadedThumb == true {

} else {
firstTimeLoadedThumb = true

}
}

} else {
if totalPosts - libraryImages.count < 50 {
allImagesLoaded = true
} else {
if firstTimeLoaded == true {
} else {
firstTimeLoaded = true

}
}


}


if thumbnail == true {
fiftyIncrementThumb = fiftyIncrementThumb + 50
increment = fiftyIncrementThumb
} else {
fiftyIncrement = fiftyIncrement + 50
increment = fiftyIncrement
}

let imageManager = PHCachingImageManager()
autoreleasepool { () -> () in

assetResults.enumerateObjectsUsingBlock { (object, count: Int, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
if object is PHAsset {

var imageSize: CGSize!

var array: NSArray!

if thumbnail == true {
array = self.libraryImageThumbnails
} else {
array = self.libraryImages
}
autoreleasepool {
if count >= array.count && count <= increment {
if thumbnail == true {
imageSize = CGSize(width: 100, height: 100)

} else {
imageSize = CGSize(width: self.cameraView.bounds.width, height: self.cameraView.bounds.height)

}

let asset = object as! PHAsset
let options = PHImageRequestOptions()
options.deliveryMode = .FastFormat
options.synchronous = true

imageManager.requestImageForAsset(asset,
targetSize: imageSize,
contentMode: .AspectFill,
options: options,
resultHandler: { (image, _: [NSObject : AnyObject]?) -> Void in
if thumbnail == true {
self.libraryImageThumbnails.append(image!)
} else {
self.libraryImages.append(image!)
}
})
}
}
}
}


}


}

我也试过这个,它返回 nil 并崩溃了......

    let library = ALAssetsLibrary()
library.enumerateGroupsWithTypes(ALAssetsGroupAll, usingBlock: { (group: ALAssetsGroup!, stop: UnsafeMutablePointer<ObjCBool>) -> Void in

group.enumerateAssetsUsingBlock({ (asset, count: Int, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
if asset != nil {
self.libraryImageThumbnails.append(asset)
self.collectionTable.reloadData()

}

})
}, failureBlock: { (error: NSError!) -> Void in

})

如果您需要更多信息,请告诉我!谢谢!

最佳答案

以下是我在一个项目中所做的可能有帮助的事情:

一些属性:

let requestOptions = PHImageRequestOptions()
let fetchOptions = PHFetchOptions()
let cachingImageManager = PHCachingImageManager()
var assets: [PHAsset] = [] {
willSet {
cachingImageManager.stopCachingImagesForAllAssets()
}

didSet {
cachingImageManager.startCachingImagesForAssets(assets,
targetSize: PHImageManagerMaximumSize,
contentMode: .AspectFit,
options: self.requestOptions
)
}
}


func fetchPhotos() {
requestOptions.resizeMode = PHImageRequestOptionsResizeMode.Exact
requestOptions.version = .Current
requestOptions.deliveryMode = PHImageRequestOptionsDeliveryMode.HighQualityFormat
requestOptions.synchronous = true

fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending: false)]
if #available(iOS 9.0, *) {
fetchOptions.fetchLimit = 50
} else {

// Fallback on earlier versions
}

fetchResults = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: fetchOptions)
guard let fetchResults = fetchResults else {
print("No PHAssets")
return
}
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) { [weak self] in
fetchResults.enumerateObjectsUsingBlock{ object, index, stop in
let asset = object as! PHAsset
self?.assets.append(asset)
}
dispatch_async(dispatch_get_main_queue()) {
self?.photoCollectionView.reloadData()
}
}
}

关于ios - 在内存不足的情况下检索图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35007766/

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