活动指示器在PHPhotoLibrary.sharedPhotoLibrary()。performChanges内行动缓慢

时间:2015-08-03 07:32:12

标签: ios swift uiactivityindicatorview photosframework

我删除了一些照片资产,我希望在资产被删除时显示活动指示,并在资产被删除时停止,但我的代码行动缓慢,你知道出了什么问题吗? / p>

PHPhotoLibrary.sharedPhotoLibrary().performChanges({
            PHAssetChangeRequest.deleteAssets(enumeration)
            self.activityIndicator.startAnimating()
            UIApplication.sharedApplication().beginIgnoringInteractionEvents()
            }, completionHandler: {success, error in
                if success {
                    self.activityIndicator.stopAnimating()
                    UIApplication.sharedApplication().endIgnoringInteractionEvents()
                    println("Success")
                } else {
                    self.activityIndicator.stopAnimating()
                    UIApplication.sharedApplication().endIgnoringInteractionEvents()
                    println("Error")
                }
        })

1 个答案:

答案 0 :(得分:3)

我自己解决了,这就是答案:

PHPhotoLibrary.sharedPhotoLibrary().performChanges({
            PHAssetChangeRequest.deleteAssets(enumeration)

            let delay = 1 * Double(NSEC_PER_SEC)
            let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
            dispatch_after(time, dispatch_get_main_queue()) {
                self.activityIndicator.startAnimating()
            }
            }, completionHandler: {success, error in

                UIApplication.sharedApplication().beginIgnoringInteractionEvents()

                if success {
                    println("good")
                    dispatch_async(dispatch_get_main_queue()){
                        self.activityIndicator.stopAnimating()
                        self.navigationController?.popToRootViewControllerAnimated(true)
                    }
                    UIApplication.sharedApplication().endIgnoringInteractionEvents()
                } else {
                    println("bad")
                    dispatch_async(dispatch_get_main_queue()){
                        self.activityIndicator.stopAnimating()
                    }
                    UIApplication.sharedApplication().endIgnoringInteractionEvents()
                }
        })

只需添加:

dispatch_async(dispatch_get_main_queue()){
     self.activityIndicator.stopAnimating()
}