如何设置 PHAsset.fetchAssets 的偏移量?
Posted
技术标签:
【中文标题】如何设置 PHAsset.fetchAssets 的偏移量?【英文标题】:How to set an offset to PHAsset.fetchAssets? 【发布时间】:2017-08-08 12:20:17 【问题描述】:这是我从设备库中获取图像的代码。
let imgManager = PHImageManager.default()
let requestOptions = PHImageRequestOptions()
//loading all the images
requestOptions.isSynchronous = false
//Quality of the image
requestOptions.deliveryMode = .highQualityFormat
//requestOptions.resizeMode = .fast
//Sorted images
let fetchOptions = PHFetchOptions()
fetchOptions.fetchLimit = 500
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
let fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)
DispatchQueue.global(qos: .background).async
if fetchResult.count > 0
DispatchQueue.main.async
self.showProgress()
for i in 0..<fetchResult.count
imgManager.requestImage(for: fetchResult.object(at: i) , targetSize: CGSize(width: self.selectedImage.frame.width, height:self.selectedImage.frame.height), contentMode: .aspectFill, options: requestOptions, resultHandler:
image, error in
if(image == nil)
else
self.imageArray.append(image!)
)
DispatchQueue.main.async
self.hideProgress()
self.photosCollectionView.reloadData()
else
self.hideProgress()
self.showDefaultAlert(title: "Alert", message: "Could not fetch images from the gallery")
当设备中有大约 1500 多个图像时,我会遇到内存问题。我已经知道如何设置一个限制,当集合视图滚动到底部时如何获取下一个 500 张图像?任何帮助将非常感激。
【问题讨论】:
【参考方案1】:你解决了什么问题?我觉得,重操作在requestImage中,你可以试试
fetchResult.objects(at: IndexSet(0...500))
fetchResult.objects(at: IndexSet(501...1000))
等等。 或者可以添加谓词
fetchOptions.predicate = NSPredicate(format: "@NOT(localIdentifier IN %@)", alreadyGettedIdentifiers)
for i in 0..<fetchResult.count
let fetchResult = fetchResult.object(at: i)
alreadyGettedIdentifiers.append(fetchResult.localIdentifier)
PHImageManager.default().requestImage(for: fetchResult, targetSize: CGSize(width: 200, height:200), contentMode: .aspectFill, options: requestOptions, resultHandler:
image, error in
if(image == nil)
else
self.imageArray.append(image!)
)
【讨论】:
以上是关于如何设置 PHAsset.fetchAssets 的偏移量?的主要内容,如果未能解决你的问题,请参考以下文章