I am developing a iPhone Photos Gallery app and In my app I created one folder into gallery and that gallery folder is shown up into my app and while use click on Image its
What is the method self.displayPhoto()
doing? Do you want to call it after pop? It seems weird.
Try adding a "return" at the line after self.navigationController?.popToRootViewControllerAnimated(true)
to make sure that the execution stops there.
When you don't have any photos left and do the popToRoot, it continues executing this lines:
alert.addAction(UIAlertAction(title: "Cancle", style: .Cancel, handler: {(alertAction)in
//Do not delete Photo
self.dismissViewControllerAnimated(true, completion: nil)
}))
self.presentViewController(alert, animated: true, completion: nil)
Try commenting them to see if the problem is there, and if it is, uncomment for solving it.
I found solution for my question is :
@IBAction func btnTrash(sender: AnyObject) {
let alert = UIAlertController(title: "Delete Image", message: "Are You Sure To delete this Image?", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Yes", style: .Default, handler: {(alertAction)in
//Delete Photo
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
let request = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection)
request.removeAssets([self.photosAsset[self.index]])
}, completionHandler: {(success, error )in
NSLog("\nDeleted Image -> %@", (success ? "Success" : "Error"))
alert.dismissViewControllerAnimated(true, completion: nil)
// here is the code for go back to root view controller
if(success){
// Move to the main thread to execute
dispatch_async(dispatch_get_main_queue(), {
self.photosAsset = PHAsset.fetchAssetsInAssetCollection(self.assetCollection, options: nil)
if(self.photosAsset.count == 0){
println("No Images Left!!")
self.navigationController?.popToRootViewControllerAnimated(true)
}else{
if(self.index >= self.photosAsset.count){
self.index = self.photosAsset.count - 1
}
self.displayPhoto()
}
})
}else{
println("Error: \(error)")
}
})
}))
alert.addAction(UIAlertAction(title: "Cancle", style: .Cancel, handler: {(alertAction)in
//Do not delete Photo
self.dismissViewControllerAnimated(true, completion: nil)
}))
self.presentViewController(alert, animated: true, completion: nil)
}
Have you tried with this line of code :
self.presentingViewController!.dismissViewControllerAnimated(true, completion: nil)