微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决从自定义相册预加载照片以滚动?

我制作了一个自定义相册 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 举报,一经查实,本站将立刻删除。