首页 > 解决方案 > 如何在 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)
    }

}

标签: iosswiftuicollectionview

解决方案


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

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

为此,添加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.countreturn 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

    }
})

推荐阅读