从照片库中仅提取选定的图像

问题描述

我正在尝试从收藏夹视图中检索从照片库中提取的照片。它工作正常,但所查看的单元格数量等于照片库中的照片数量而不是从图库中获取的照片数量。 这是一个示例代码和屏幕截图,可以更好地理解我的问题。


var fetchedResults : PHFetchResult<PHAsset>! let imageManager = PHCachingImageManager()

    func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
        return fetchedResults.count
    }
    
    func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let imageCell = collectionView.dequeueReusableCell(withReuseIdentifier: PhotosId,for: indexPath) as! PhotosCollectionViewCell

        let asset = fetchedResults.object(at: indexPath.item)
        
        imageCell.representedAssetId = asset.localIdentifier

        imageManager.requestimage(for: asset,targetSize: CGSize(width: 90,height: 90),contentMode: .aspectFit,options: requestOptions()) { (image,_) in
            if imageCell.representedAssetId == asset.localIdentifier {
        if imageCell.representedAssetId == asset.localIdentifier {
                    imageCell.photosImageView.image = image
            }
          }
        }
         imageCell.backgroundColor = .white

        return imageCell
        }


    func fetchAssets() {  
        fetchedResults = PHAsset.fetchAssets(with: .ascendingOptions)
 
    }
    
    func requestOptions() -> PHImageRequestOptions {
        let deliveredImage = PHImageRequestOptions()
        deliveredImage.isSynchronous = true
        deliveredImage.deliveryMode = .highQualityFormat
        return deliveredImage
        
    }
 }


extension imageSelectedVC: PHPhotoLibraryChangeObserver {
    func photoLibraryDidChange(_ changeInstance: PHChange) {
      
        guard let changes = changeInstance.changeDetails(for: fetchedResults)
        else { return }
        
      
        self.fetchedResults = changes.fetchResultAfterChanges
        
       
        dispatchQueue.main.async {
           
            self.fetchedResults = changes.fetchResultAfterChanges
            if changes.hasIncrementalChanges {
 
                guard self.LibraryPhotosCollectionView != nil else { fatalError() }
                self.LibraryPhotosCollectionView.performBatchUpdates({
                   
                    if let removed = changes.removedindexes,!removed.isEmpty {
                        self.LibraryPhotosCollectionView.deleteItems(at: removed.map({ IndexPath(item: $0,section: 0) }))
                    }
                    
                    if let inserted = changes.insertedindexes,inserted.count > 0 {
                        self.LibraryPhotosCollectionView.insertItems(at: [IndexPath(item: self.fetchedResults.count - 1,section: 0)])
                    
                    }
                   
                    if let changed = changes.changedindexes,!changed.isEmpty {
                        self.LibraryPhotosCollectionView.reloadItems(at: changed.map({ IndexPath(item: $0,section: 0) }))
                    }
                    
    changes.enumerateMoves { fromIndex,toIndex in
                        self.LibraryPhotosCollectionView.moveItem(at: IndexPath(item: fromIndex,section: 0),to: IndexPath(item: toIndex,section: 0))
                    }
                })
           
            } else {
                self.LibraryPhotosCollectionView.reloadData()
            }
        }
    }
}

extension PHFetchOptions {
    static var ascendingOptions: PHFetchOptions = {
        let option = PHFetchOptions()
        option.sortDescriptors = [NSSortDescriptor(key: "creationDate",ascending: true)]
        return option
    }()
}

enter image description here

  • 白细胞是我要获得的细胞数量
  • 出现的照片是我从图书馆中获取的照片

我认为问题出在fetchedResults.count上,但我不确定如何正确解决问题

解决方法

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

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

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