gpt4 book ai didi

ios - 如何在 UICollectionView 中对照片库中的图像进行分页

转载 作者:行者123 更新时间:2023-11-29 13:55:34 28 4
gpt4 key购买 nike

我已经从 UICollectionView 中的 photoLibrary 导入了图像,而 collectionView 需要很多时间来初始化 collectionView 中的照片,所以我需要首先通过垂直滚动对这些图像进行分页我想在 collectionView 中加载 10 张图像,然后用户分页加载10张图片,过程继续

import UIKit
import Photos
import XLPagerTabStrip

private let reuseIdentifier = "Cell"

class PhotosRecyclerCollection: UICollectionViewController, UICollectionViewDelegateFlowLayout, UINavigationControllerDelegate {

var imageArray = [UIImage]()

override func viewDidLoad() {
grabPhotos()
}

func grabPhotos(){

let imgManager = PHImageManager.default()

let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = true
requestOptions.deliveryMode = .highQualityFormat

let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

if let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions) {

if fetchResult.count > 0 {

for i in 0..<fetchResult.count{

imgManager.requestImage(for: fetchResult.object(at: i) as! PHAsset, targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: {

image , error in

self.imageArray.append(image!)
})
}
}

else {

print("You got no photos")

self.collectionView?.reloadData()
}
}

}





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

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotosCollectionViewCell", for: indexPath) as! PhotosCollectionViewCell

let imageView = cell.viewWithTag(1) as! UIImageView

imageView.image = imageArray[indexPath.row]

return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

let width = collectionView.frame.width / 4 - 1


return CGSize(width: width, height: width)



}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 1.0
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 1.0
}

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let mainStoryBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let desVC = mainStoryBoard.instantiateViewController(withIdentifier: "imageCropper") as! ImageCropperViewController
desVC.image = imageArray[indexPath.row]
self.navigationController?.pushViewController(desVC, animated: true)
}

}

最佳答案

您不需要分页。加载需要很长时间,因为您要预加载所有图像。

简单地停止在内存中预加载图像,并在实际需要时获取相关图像。

为此添加fetchResult 作为实例变量:

private var fetchResult: PHFetchResult?

替换

if let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions) {

if fetchResult.count > 0 {

for i in 0..<fetchResult.count{

imgManager.requestImage(for: fetchResult.object(at: i) as! PHAsset, targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: {

image , error in

self.imageArray.append(image!)
})
}
}

if let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)
{
self.fetchResult = fetchResult
}

return imageArray.count 替换为 return self.fetchResult?.count ?? 0

最后:

let imageView = cell.viewWithTag(1) as! UIImageView
imageView.image = imageArray[indexPath.row]

imgManager.requestImage(for: fetchResult!.object(at: i) as! PHAsset, targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: { image , error in
let currentIndexPath = collectionView.indexPath(for: cell)
if (currentIndexPath.compare(indexPath) == .orderedSame) {
let imageView = cell.viewWithTag(1) as! UIImageView
imageView.image = image

}
})

关于ios - 如何在 UICollectionView 中对照片库中的图像进行分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56406892/

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