gpt4 book ai didi

ios - 使用 PHAsset 获取用户照片库时如何分页

转载 作者:搜寻专家 更新时间:2023-11-01 06:26:03 25 4
gpt4 key购买 nike

我问的问题与 here 相同我不明白如何实现解决方案。我已经尝试了以下

fileprivate func fetchPhotos(indexSet: IndexSet) {
let allPhotos = PHAsset.fetchAssets(with: .image, options: assetsFetchOptions())
DispatchQueue.global(qos: .background).async {
allPhotos.enumerateObjects(at: indexSet, options: NSEnumerationOptions.concurrent, using: { (asset, count, stop) in
let imageManager = PHImageManager.default()
let targetSize = CGSize(width: 200, height: 200)
let options = PHImageRequestOptions()
options.isSynchronous = true
imageManager.requestImage(for: asset, targetSize: targetSize, contentMode: .aspectFit, options: options, resultHandler: { (image, info) in
if let image = image {
self.images.append(image)
self.assets.append(asset)

if self.selectedImage == nil {
self.selectedImage = image
}
}
DispatchQueue.main.async {
self.collectionView.reloadData()
self.hud.dismiss()
}

})
})
}
}

在 cellForItemAt 中,我尝试将索引加倍,以便加载接下来的 10 个。我得到的结果是前 10 个帖子永无止境地重复。有人可以展示使用它的正确方法吗?

最佳答案

我试着做你想做的。我无法将它封装在一个函数中,因为它需要一些公共(public)变量,所以这里是一个 UIViewController 的代码,它有一个 UICollectionView,它以 10 页为单位加载图像,当滚动到最后一个单元格时,它加载接下来的 10 张图像等等。

import UIKit
import Photos
import PhotosUI

class ImagesViewController: UIViewController ,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{
var assets = [PHAsset]()
var images = [UIImage]()
let page = 10
var beginIndex = 0

var endIndex = 9
var allPhotos : PHFetchResult<PHAsset>?
var loading = false
var hasNextPage = false

@IBOutlet weak var collectionView : UICollectionView!


override func viewDidLoad() {
super.viewDidLoad()
let options = PHFetchOptions()
options.includeHiddenAssets = true
allPhotos = PHAsset.fetchAssets(with: .image, options: options)
getImages()
}

func getImages(){
endIndex = beginIndex + (page - 1)
if endIndex > allPhotos!.count {
endIndex = allPhotos!.count - 1
}
let arr = Array(beginIndex...endIndex)

let indexSet = IndexSet(arr)
fetchPhotos(indexSet: indexSet)
}


fileprivate func fetchPhotos(indexSet: IndexSet) {

if allPhotos!.count == self.images.count {
self.hasNextPage = false
self.loading = false
return
}

self.loading = true

DispatchQueue.global(qos: .background).async { [weak self] in
self?.allPhotos?.enumerateObjects(at: indexSet, options: NSEnumerationOptions.concurrent, using: { (asset, count, stop) in

guard let weakSelf = self else {
return
}

let imageManager = PHImageManager.default()
let targetSize = CGSize(width: weakSelf.view.frame.size.height - 20, height: 250)
let options = PHImageRequestOptions()
options.isSynchronous = true
imageManager.requestImage(for: asset, targetSize: targetSize, contentMode: .aspectFit, options: options, resultHandler: { (image, info) in
if let image = image {
weakSelf.images.append(image)
weakSelf.assets.append(asset)
}

})
if weakSelf.images.count - 1 == indexSet.last! {
print("last element")
weakSelf.loading = false
weakSelf.hasNextPage = weakSelf.images.count != weakSelf.allPhotos!.count
weakSelf.beginIndex = weakSelf.images.count
DispatchQueue.main.async {
weakSelf.collectionView.reloadData()
}
}
})
}
}

func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
let imgView = cell.viewWithTag(1) as! UIImageView
imgView.image = self.images[indexPath.row]

if self.hasNextPage && !loading && indexPath.row == self.images.count - 1 {
getImages()
}

return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width:collectionView.frame.size.width - 20,height: 250)


}
}

关于ios - 使用 PHAsset 获取用户照片库时如何分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54560446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com