Allowing picture taking

Now we are ready to move onto the programming. The first thing we need to allow the user to do is to take a new picture. In order to do that, we are going to need some code to run every time the user taps on the add button. We achieve this by connecting the trigger action of the add button to a method on our view controller. Normally we make a connection by right-click dragging from the button to the code; however, we can't do this if we can't see the interface and the code at the same time. The easiest way to do this is to show the Assistant Editor. You can do this by navigating to View | Assistant Editor | Show Assistant Editor. Also, make sure it is configured to be automatic by clicking on the bar at the top of the editor:

Allowing picture taking

This mode causes the second view to automatically change to the most appropriate file according to what you have selected on the left. In this case, because we are working with the interface of our view controller, it shows the code for the view controller.

Our view controller code is generated with two methods to start. viewDidLoad is called when the view for the view controller is loaded. Most of the time this happens when the view controller is about to be displayed for the first time. didReceiveMemoryWarning is called when the system starts to run low on memory. This provides you an opportunity to help the system find more memory by deleting anything that isn't necessary.

We want to start by creating a connection from the button to a new method. You can do so by right clicking on the add button and dragging to below the didReceiveMemoryWarning method:

Allowing picture taking

When you release the right mouse button, a little window will appear. There you should select Action from the Connection menu and enter didTapTakePhotoButton. When you click on Connect, Xcode will create a new method for you and connect it to the button. You know it is connected because there is a filled in gray circle to the left of the method. Now, every time the user taps the button, this method will be executed. Note that this method has @IBAction at the beginning of it. This is needed for any method that is connected to an interface element.

We want this method to present the user with an interface to take a picture. Apple provides a class for us called UIImagePickerController that makes this very easy for us. All we need to do is create an instance of UIImagePickerController, configure it to allow taking pictures, and present it to the screen. The code looks like this:

@IBAction func didTapTakePhotoButton(sender: AnyObject) {
    let imagePicker = UIImagePickerController()
    if UIImagePickerController.isSourceTypeAvailable(.Camera) {
        imagePicker.sourceType = .Camera
    }
    self.presentViewController(
        imagePicker,
        animated: true,
        completion: nil
    )
}

Lets break this code down. On the first line, we are creating our image picker. On the second line, we are checking if the current device has a camera by using the isSourceTypeAvailable: class method of UIImagePickerController. If the camera source is available, we set that as the source type for the image picker on line three. Otherwise, by default, an image picker lets the user pick an image from their photo library. Since the simulator doesn't support taking a picture, you are going to be presented with an image picker instead of a camera when simulating the app. Finally, the last line asks our view controller to present our image picker by animating it on the screen. presentViewController:animated:completion: is a method implemented within the UIViewController class, the superclass of our ViewController, to make it easy for us to present new view controllers. If you run the app and click on the add button, you will be asked for permission to access the photos and then it will display the photo picker. You can tap the Cancel button in the upper right and the image picker controller will be dismissed. However, if you select a photo, nothing will happen.

We need to write some code to handle the picking of a photo. To make this possible, image picker can have a delegate that receives a method call when an image is picked. We are going to make our view controller the delegate of the image picker and implement its protocol. First, we have to add a line to our action method above, that assigns our view controller as the delegate of the image picker. Add this line above the call to present the image picker:

imagePicker.delegate = self

When we do that, we will get a compiler error that says that we can't make this assignment because our view controller doesn't implement the necessary protocols. Lets change that. I like to implement each protocol as a separate extension in the same file to allow for better code separation. We need to implement both UIImagePickerControllerDelegate and UINavigationControllerDelegate according to the error. The only method that is important to us in either of these protocols is the one that is called when an image is picked. That leaves us with the following code:

extension ViewController: UINavigationControllerDelegate {}

extension ViewController: UIImagePickerControllerDelegate {
    func imagePickerController(
        picker: UIImagePickerController,
        didFinishPickingImage image: UIImage!,
        editingInfo: [NSObject : AnyObject]!
        )
    {
        self.dismissViewControllerAnimated(true, completion: nil)
    }
}

Our implementation for the UINavigationControllerDelegate delegate is empty but we have a simple implementation for the imagePickerController:picker:didFinishPickingImage:editingInfo: method. This is where we are going to add our handling code, but for now, we are just dismissing the presented view controller to return the user to the previous screen. This method does not force us to specify the view controller we are dismissing because the view controller already knows which one it is presenting. Now, if you run the app and select a photo, you will return to the previous screen but nothing else will happen. In order to make something meaningful happen with the photo, we are going to have to put a lot of other code in place. We have to both save the picture and implement our view controller to display the picture inside our collection view.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset