如何从UICollectionView中的照片库对图像进行分页

时间:2019-06-01 12:45:53

标签: ios swift 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)
    }

}

1 个答案:

答案 0 :(得分:0)

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

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

为此,将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

    }
})