ios - 如何在 UICollectionView 中对照片库中的图像进行分页
问题描述
我已经从 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
}
})
推荐阅读
- c# - 将 SqlConnection 作为 IDbConnection 返回
- c# - 如何在asp.net mvc中修复404“找不到资源”
- google-apps-script - 删除最后一个非空行之后的所有行
- java - 如何将 HTML 中的 href 值传递给 Thymeleaf?
- python - 从 api dict 响应中获取价值
- caching - .NET Core 下拉 NuGet 包到用户缓存
- python - 作为命令行参数传入时,三重积分函数不起作用
- c# - 在按钮单击时将列表视图中的行分配给字典
- google-analytics - 通过 Measurement Protocol 发送的一些 Google Analytics(分析)事件在一天后消失
- excel - Excel 数据透视图:显示最后 25 行