Warning: Illegal string offset 'value' in /home/content/44/9038144/html/deegeu/wp-content/themes/Divi/includes/builder/main-modules.php on line 14095

Warning: Illegal string offset 'value' in /home/content/44/9038144/html/deegeu/wp-content/themes/Divi/includes/builder/main-modules.php on line 14095

Question: What is the the imageWasSavedSuccessfully function?

by | Jun 30, 2016




Transcript – Question: What is the the imageWasSavedSuccessfully function?

In this video, we’ll take a quick look at the Swift method imageWasSavedSuccessfully when taking pictures with our iOS camera.

Hi, I’ve been getting many questions in the comments, so I figure it’s time we start answering some in their own videos.

Question: What is the the imageWasSavedSuccessfully function?

In the “How to access the iOS camera using Swift 2.0”, 00bikeboy asks “The transcript doesn’t go into the imageWasSavedSuccessfully function. Any chance you could go over this part of the code?” Absolutely! Let’s take a deeper look at the method.

If you remember from the Swift iOS camera tutorial, these were the basic steps to taking a photo. We get access, check to see if we can use the camera, present a camera view controller, check to see if we have an image, and then do something with it. The area we’re focusing on for this question is what we do with the image.

Steps to access camera

Steps to access camera

What we did in the video was save the image to the photos album. iOS calls didFinishPickingMediaWithInfo once we have selected a photo, either by selection or taking a photo. We then called UIImageWriteToSavedPhotosAlbum. This saves the photo to the photo album, and from there we can provide another function to call once the image is written.

func imagePickerController(picker: UIImagePickerController, 
                    didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    print("Got an image")

    if let pickedImage:UIImage = (info[UIImagePickerControllerOriginalImage]) as? UIImage {

        let selectorToCall = Selector("imageWasSavedSuccessfully:didFinishSavingWithError:context:")
        UIImageWriteToSavedPhotosAlbum(pickedImage, self, selectorToCall, nil)
    }

    imagePicker.dismissViewControllerAnimated(true, completion: {
        // Anything you want to happen when the user saves an image
    })

}

The selector is imageWasSavedSuccessfully. This is a user defined function, and we can put anything we want into it.

func imageWasSavedSuccessfully(image: UIImage, didFinishSavingWithError error: NSError!, 
                               context: UnsafeMutablePointer<()>){

    print("Image saved")
    if let theError = error {
        print("An error happened while saving the image = \(theError)")
    } else {
        print("Displaying")
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
             self.currentImage.image = image
        }       
    }
}

What we put in the function was a check to make sure it was successfully saved. If it didn’t we’d get an error object. If not, the image was saved so I’m updating the UI to display the image on a background thread.

So that’s what we were doing in the imageWasSavedSuccessfully selector. To recap, it’s a user defined selector we passed to UIImageWriteToSavedPhotosAlbum. The method can do anything we want, but in our case we just updated the UI with the new image.

If you have any other questions about this video, or any others, leave a comment below or you can also post questions on DeegeU.com. If you want to keep up to date with the latest DeegeU tutorials, make sure you subscribe to the YouTube channel, like the DeegeU Facebook page, or sign up for the DeegeU.com newsletter.

See you in the next video!




Related Posts

Tools Used

  • Swift
  • Xcode
  • iOS

Media Credits

All media created and owned by DJ Spiess unless listed below.

  • No infringement intended

Music: Funk Down – Free audio on YouTube.com

Get the code

The source code for “Question: What is the the imageWasSavedSuccessfully function?” can be found on Github. If you have Git installed on your system, you can clone the repository by issuing the following command:

 git clone https://github.com/deege/deegeu-swift-camera-basic.git

Go to the Support > Getting the Code page for more help.

If you find any errors in the code, feel free to let me know or issue a pull request in Git.

Don't miss another video!

New videos come out every week. Make sure you subscribe!

Comments

comments

DJ Spiess

DJ Spiess

Your personal instructor

My name is DJ Spiess and I'm a developer with a Masters degree in Computer Science working in Colorado, USA. I primarily work with Java server applications. I started programming as a kid in the 1980s, and I've programmed professionally since 1996. My main focus are REST APIs, large-scale data, and mobile development. The last six years I've worked on large National Science Foundation projects. You can read more about my development experience on my LinkedIn account.

Pin It on Pinterest

Share This