如何设置PHAsset.fetchAssets的偏移量?

时间:2017-08-08 12:20:17

标签: ios swift3 phfetchoptions

这是我从设备库中获取图片的代码。

let imgManager = PHImageManager.default()
    let requestOptions = PHImageRequestOptions()

    //loading all the images
    requestOptions.isSynchronous = false

    //Quality of the image
    requestOptions.deliveryMode = .highQualityFormat

    //requestOptions.resizeMode = .fast

    //Sorted images
    let fetchOptions = PHFetchOptions()
    fetchOptions.fetchLimit = 500

    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

    let fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)

 DispatchQueue.global(qos: .background).async {

        if fetchResult.count > 0 {

            DispatchQueue.main.async {
                self.showProgress()
            }

            for i in 0..<fetchResult.count{

                imgManager.requestImage(for: fetchResult.object(at: i) , targetSize: CGSize(width: self.selectedImage.frame.width, height:self.selectedImage.frame.height), contentMode: .aspectFill, options: requestOptions, resultHandler: {
                    image, error in

                    if(image == nil){
                    }else{
                        self.imageArray.append(image!)
                    }
                })
            }

            DispatchQueue.main.async {
                self.hideProgress()
                self.photosCollectionView.reloadData()
            }

        }else{
            self.hideProgress()
            self.showDefaultAlert(title: "Alert", message: "Could not fetch images from the gallery")

        }

当设备中有大约1500张图像时,我会遇到内存问题。我已经想出如何设置一个限制,当集合视图滚动到底部时,如何获得下500个图像?任何帮助将非常感激。

1 个答案:

答案 0 :(得分:0)

你修理的问题是什么?我认为,重要的操作是在requestImage中,你可以尝试

[{"RowNum":1,"TotalCount":18,"Id":39,"UserName":"adsbkj","Password":"23123"},{"RowNum":2,"TotalCount":18,"Id":40,"UserName":"adsbkj","Password":"23123"},{"RowNum":3,"TotalCount":18,"Id":37,"UserName":"asd","Password":"23"},{"RowNum":4,"TotalCount":18,"Id":2,"UserName":"asd","Password":"asdasd"},{"RowNum":5,"TotalCount":18,"Id":5,"UserName":"asd","Password":"asda"},{"RowNum":6,"TotalCount":18,"Id":6,"UserName":"asd","Password":"asda"},{"RowNum":7,"TotalCount":18,"Id":42,"UserName":"das","Password":"asd"},{"RowNum":8,"TotalCount":18,"Id":44,"UserName":"f","Password":"DSD"},{"RowNum":9,"TotalCount":18,"Id":38,"UserName":"fs","Password":"dasda"},{"RowNum":10,"TotalCount":18,"Id":7,"UserName":"hunain","Password":"123"},{"RowNum":11,"TotalCount":18,"Id":8,"UserName":"hunain","Password":"123"},{"RowNum":12,"TotalCount":18,"Id":41,"UserName":"nkjn","Password":"fg"},{"RowNum":13,"TotalCount":18,"Id":4,"UserName":"rqads","Password":"12"},{"RowNum":14,"TotalCount":18,"Id":3,"UserName":"Tester","Password":"123"},{"RowNum":15,"TotalCount":18,"Id":9,"UserName":"username","Password":"123"},{"RowNum":16,"TotalCount":18,"Id":45,"UserName":"ven","Password":"324"},{"RowNum":17,"TotalCount":18,"Id":43,"UserName":"xf","Password":"sad"},{"RowNum":18,"TotalCount":18,"Id":1,"UserName":"Yasir","Password":"123"}]

等。 或者可以添加谓词

fetchResult.objects(at: IndexSet(0...500))
fetchResult.objects(at: IndexSet(501...1000))