使用PHAsset提取用户照片库时如何分页 [英] How To Paginate when using PHAsset to Fetch User Photo library

查看:136
本文介绍了使用PHAsset提取用户照片库时如何分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在问与此处< 我不明白如何实施解决方案. 我尝试了以下

I am asking the same question as here I do not understand how to implement to solution. I have tried the following

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个帖子的永无止境的重复. 有人可以显示使用此方法的正确方法吗?

In cellForItemAt I tried doubling the indexes so the next 10 would load. The result i got was a never ending repetition of the first 10 posts. Can someone please show the proper way to use this.

推荐答案

我试图做您想做的事情.我无法将其封装在一个函数中,因为它需要几个公共变量,所以这是UIViewController的代码,该代码具有一个UICollectionView,该页面以10页的页面加载图像,并滚动到最后一个单元格时,它加载接下来的10张图像等等.

I tried to do what you want. I couldn't encapsulate it inside one function, coz it requires a few public variables, so here is the code for a UIViewController that has a UICollectionView which loads images by a page of 10 and when scrolled to the last cell it loads next 10 images and so on.

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)


    }
}

这篇关于使用PHAsset提取用户照片库时如何分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆