Как разбить на страницы при использовании PHAsset для извлечения библиотеки фотографий пользователя

Я задаю тот же вопрос, что и здесь Я не понимаю, как реализовать решение. Я пробовал следующее

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 постов. Может кто-нибудь показать правильный способ использования этого.


person user6520705    schedule 06.02.2019    source источник


Ответы (2)


Я пытался сделать то, что ты хочешь. Я не мог инкапсулировать его в одну функцию, потому что для этого требуется несколько общедоступных переменных, поэтому вот код для UIViewController, который имеет UICollectionView, который загружает изображения на страницу из 10, а при прокрутке до последней ячейки он загружает следующие 10 изображений. и так далее.

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)


    }
}
person Shahzaib Qureshi    schedule 15.02.2019
comment
единственное, что я получаю, этот UIView.frame должен использоваться из основного потока только в функции извлечения фотографий - person user6520705; 19.02.2019

Уточнение и проблемы решены, теперь ниже код работает нормально

импортировать фотографии импортировать PhotosUI

класс PhotoGalleryPaginationViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

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!

// MARK :- LIFE CYCLE
override func viewDidLoad() {
    super.viewDidLoad()

    // Cell Register
    collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")

    // Fetch Gallery
    let options = PHFetchOptions()
    options.includeHiddenAssets = true
    allPhotos = PHAsset.fetchAssets(with: .image, options: options)
    getImages()
    // Do any additional setup after loading the view.
}

// MARK :- Methods
func getImages(){
    endIndex = beginIndex + (page - 1)
    if endIndex > allPhotos!.count {
        endIndex = allPhotos!.count - 1
    }
    if endIndex > beginIndex {
        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
    let targetSize = CGSize(width: 200, height: 200)
    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 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()
                }
            }
        })
    }
}

// MARK :- CollectionView DataSource and Delegate
func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    print("image Count = \(images.count)")
    return images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

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

    let imgView = UIImageView()
    imgView.frame = CGRect(x: 0, y: 0, width: 150, height: 150)
    imgView.image = self.images[indexPath.row]
    cell.contentView.addSubview(imgView)
    cell.clipsToBounds = true

    // Fetch new Images
    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.width/2,height: 150)
}

}

person Tajinder singh    schedule 15.11.2019