Search code examples
iosswiftuiimagescreenshotalert

Using completionSelector and completionTarget with UIImageWriteToSavedPhotosAlbum


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)
}

Solution

  • import UIKit
    
    class ViewController: UIViewController {
    
       @IBAction func buttonPressed(sender: AnyObject) {
          presentImagePickerController()
       }
    
    }
    
    extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
       func presentImagePickerController() {
          let imagePickerController = UIImagePickerController()
          imagePickerController.delegate = self
          imagePickerController.sourceType = .PhotoLibrary
          imagePickerController.allowsEditing = false
    
          presentViewController(imagePickerController, animated: true, completion: nil)
       }
    
       func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
          UIImageWriteToSavedPhotosAlbum(image, self, "image:didFinishSavingWithError:contextInfo:", nil)
       }
    
       func imagePickerControllerDidCancel(picker: UIImagePickerController) {
          self.dismissViewControllerAnimated(true, completion: nil)
       }
    
       func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
          guard error == nil else {
             //Error saving image
             return
          }
          //Image saved successfully
       }
    
    }