【问题标题】:How to set an offset to PHAsset.fetchAssets?如何设置 PHAsset.fetchAssets 的偏移量?
【发布时间】:2017-08-08 12:20:17
【问题描述】:

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

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 张图像?任何帮助将非常感激。

【问题讨论】:

    标签: ios swift3 phfetchoptions


    【解决方案1】:

    你解决了什么问题?我觉得,重操作在requestImage中,你可以试试

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

    等等。 或者可以添加谓词

    fetchOptions.predicate = NSPredicate(format: "@NOT(localIdentifier IN %@)", alreadyGettedIdentifiers)
    
    for i in 0..<fetchResult.count{
                let fetchResult = fetchResult.object(at: i)
                alreadyGettedIdentifiers.append(fetchResult.localIdentifier)
    
                PHImageManager.default().requestImage(for: fetchResult, targetSize: CGSize(width: 200, height:200), contentMode: .aspectFill, options: requestOptions, resultHandler: {
                    image, error in
    
                    if(image == nil){
                    }else{
                     self.imageArray.append(image!)
    
                    }
                })
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-28
      • 1970-01-01
      相关资源
      最近更新 更多