I'm trying to figure out a way to have my iOS app save a screenshot to the Camera Roll and then pop up an alert to tell the user that the screenshot was saved successfully.
The only way I can think to do this would be with some form of an if/else loop (as you'll see in my pseudocode comments below), but I can't think of any syntax to work with the UIImageWriteToSavedPhotosAlbum
function from UIKit. I've come across suggestions that say to use completionSelector and completionTarget on Apple's development site, but I don't really understand how to use them or what specific diction I should use for the completionSelector
and completionTarget
in my code. I'm relatively new to Swift.
Can someone explain how they work and how I can find the syntax for using them in my code?
func screenshotMethod()
{
UIGraphicsBeginImageContextWithOptions(HighchartsView.scrollView.contentSize, false, 0);
view.layer.renderInContext(UIGraphicsGetCurrentContext())
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
//if saved to Camera Roll
// {
// confirmScreenshot()
// }
//
//else
// {
// exit code/stop
// }
}
func confirmScreenshot()
{
let alertController = UIAlertController(title: "Success", message: "This chart has been successfully saved to your Camera Roll.", preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
alertController.addAction(defaultAction)
presentViewController(alertController, animated: true, completion: nil)
}
Here is a working example for saving a screenshot of the current view in camera roll working in SWIFT 3.0
}
@Dare's solution for checking error didn't work for me (Swift 2.0).
This slight modification did work:
Another example in swift 2.2
The key difference in this example is that the class that has the callback function has to extend from NSObject, (@objc may work too)