2

这是我从 PhotoLibrary 获取所有资产的功能。

// MARK: Album photos

    func getPhotos() {
        ImageManger.photoAuthorization { (granted: Bool) in
            if granted {
                // Asynchronous get photos, avoid taking the main thread
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
                    self.photoGroups = ImageManger.photoLibrarys()
                    if self.photoGroups?.count == 0 {
                        return
                    }

                    let group = self.photoGroups?[0]
                    self.photoAssets = ImageManger.photoAssetsForAlbum(group!)
                    for asset: PHAsset in self.photoAssets! {
                        ImageManger.imageFromAsset(asset, isOriginal: false, toSize: CGSize(width: 150, height: 150), resultHandler: { (image: UIImage?) in
                            guard image != nil else {
                                return
                            }
                            self.photos.append(image!)
                        })
                    }

                    dispatch_async(dispatch_get_main_queue(), {
                        // Reload data
                        self.albumCollection.reloadData()
                    })
                })
            } else {
                self.presentAccessRestrictedMessage()
            }
        }
    }

我在 a 中调用getPhotos(),并且在 App 进入前台时调用,以便在捕获资产时同步资产。它工作得很好。viewWillAppearTabViewControllergetPhotos

问题是,当我在 PhotoLibrary 中有 500 多个资产时,加载需要几秒钟。到那时,如果我关闭应用程序并再次重新打开,或者,如果我切换选项卡并返回到同一个控制器,即,如果我强制getPhotos()调用,photos数组就会搞砸!

当闭包仍在运行时,如果我调用getPhotos(),则会复制数组元素。我该如何处理这种情况?

4

0 回答 0