我想从手机中获取所有图像。在照片库中创建多个选项。我使用下面的代码从手机中获取所有图像。它工作得很好,但是当在手机上有1000张图像时,它需要更多时间来获取所有图像并在视图中显示。如何减少从手机获取所有图像的时间。
// get all images from photos
func getAllImagesFromPhotos() -> [Photos]?
{
let imgManager = PHImageManager.default()
let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = true
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending: true)]
let fetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fetchOptions)
var allImages = [Photos]()
for index in 0..<fetchResult.count
{
let asset = fetchResult.object(at: index) as PHAsset
imgManager.requestImage(for: asset, targetSize: CGSize(width:200,height:200), contentMode: .aspectFill, options: requestOptions, resultHandler: { (uiimage, info) in
if let image = uiimage
{
allImages.append(Photos(image: image, selected: false))
}
})
}
return allImages
}