从自定义相册预加载照片以滚动?

问题描述

我制作了一个自定义相册 CollectionView,其中根据用户的相册选择显示所有照片。我的问题是我一次上传所有照片(例如,600 张照片需要 6 秒,我尝试选择自定义尺寸,这样质量不会丢失)。但据我所知,我需要用户将照片上传到收藏卷轴。我发现我需要使用 prefetchDataSource 来找出现在将显示哪些索引或在 willdisplayCellUICollectionView 方法显示哪些索引。 告诉我如何更好地实现这一点,我将感谢示例代码。 下面是我打开相册时上传所有照片的方法

func getPhotosFromAlbum(albumName: String) {
    var photoLibraryImages = [UIImage]()
    var photoLibraryAssets = [PHAsset]()
    
    dispatchQueue.main.async {
        let fetchOptions = PHFetchOptions()
        fetchOptions.predicate = nspredicate(format: "mediaType = %d",PHAssetMediaType.image.rawValue)
        
        let smartAlbums = PHAssetCollection.fetchAssetCollections(with: .smartAlbum,subtype: .any,options: nil)
        let customAlbums = PHAssetCollection.fetchAssetCollections(with: .album,options: nil)
        
        [smartAlbums,customAlbums].forEach {
            $0.enumerateObjects { [self] collection,index,stop in
                
                let imgManager = PHCachingImageManager()
                
                let requestOptions = PHImageRequestOptions()
                requestOptions.isSynchronous = true
                requestOptions.deliveryMode = .fastFormat

                let photoInAlbum = PHAsset.fetchAssets(in: collection,options: fetchOptions)
                
                if let title = collection.localizedTitle
                {
                    if title == albumName
                    {
                        if photoInAlbum.count > 0
                        {
                                for i in (0..<photoInAlbum.count).reversed()
                                {
                                    let imageSize = CGSize(width: customWidth,height: customHeight)

                                    imgManager.requestimage(for: photoInAlbum.object(at: i) as PHAsset,targetSize: imageSize,contentMode: .aspectFit,options: requestOptions,resultHandler: {
                                        image,error in
                                        if image != nil
                                        {
                                            photoLibraryImages.append(image!)
                                            photoLibraryAssets.append(photoInAlbum.object(at: i))
                                        }
                                    })
                                }               
                            }
                        }
                }
            }
        }
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)