如何解决从照片库中仅提取选定的图像
我正在尝试从收藏夹视图中检索从照片库中提取的照片。它工作正常,但所查看的单元格数量等于照片库中的照片数量而不是从图库中获取的照片数量。 这是一个示例代码和屏幕截图,可以更好地理解我的问题。
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
}()
}
我认为问题出在fetchedResults.count上,但我不确定如何正确解决问题
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。