Making pictures with your app is a great feature. Can you imagine how Facebook will look like without it? And Instagram probably would not even exist. I will put myself to the test and explain how I use the UIImagePickerController class in my apps.
If you like to have information about a certain class, it's wisefull to start with Xcode's documentation instead of looking on the world wide web for examples. This will make you understand the class more thoroughly, and more options will be available for you.
As it describes in it's into, the class will manage interfaces to take pictures and movies. You also have the option to choose these from your library. On top of that, it manages interactions and provide the result to a delegate.
To make it easier to understand the class, I will use the following example:
func takePicture () {
let imagePicker = UIImagePickerController()
if UIImagePickerController.isSourceTypeAvailable(.Camera) {
imagePicker.sourceType = .Camera
}
else {
imagePicker.sourceType = .PhotoLibrary
}
imagePicker.delegate = self
presentViewController(imagePicker, animated: true, completion: nil)
}
So first, we create a function called takePicture(). We create a variable and assign it an instance of the UIImagePickerController class. Now, we can use that variable to get to the methods of the class.
First we check whether the device has a camera. We do so by calling the isSourceTypeAvailable method. If the device has a camera, it will pick the camera. Otherwise, it will choose from the library.
Step two is to set the delegate. Let the class, for example UIViewController, know by assigning the UIImagePickerControllerDelegate protocol. Your class will look something like this:
class viewController: UIViewController, UIImagePickerControllerDelegate {
…
}
We assign the delegate of the imagePicker to self.
It’s time to place the image on the screen. The method called presentViewController:animated:completion: is the one we are looking for.
This is a simple breakdown how to use the UIImagePickerController class, but there is more !
What we didn’t touch yet, is how to dismiss the Camera function. You will need this in order to have a positive user experience.
Let’s check below code:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: AnyObject]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
imageView.userInteractionEnabled = true
imageView.image = image
dismissViewControllerAnimated(true, completion: nil)
}
imagePickerController:didFinishPickingMediaWithInfo: is a delegate method, which you need in order to dismiss the camera after the picture is taken.
First we tell it that we created a picture (as UIImage). Now we can assign that image to the imageView. In the meantime, we can make it so that we can re-take a picture if the shown result is not something we like.
Last, but not least, the imagepicker will be removed from the screen with the dismiss method:
dismissViewControllerAnimated(true, completion: nil)
So far an introduction in the usage of UIImagePickerController, maybe in the future we will spice it up a notch with capturing video, customized camera controls or other cool stuff we can do with this class.
If you like to have information about a certain class, it's wisefull to start with Xcode's documentation instead of looking on the world wide web for examples. This will make you understand the class more thoroughly, and more options will be available for you.
As it describes in it's into, the class will manage interfaces to take pictures and movies. You also have the option to choose these from your library. On top of that, it manages interactions and provide the result to a delegate.
To make it easier to understand the class, I will use the following example:
func takePicture () {
let imagePicker = UIImagePickerController()
if UIImagePickerController.isSourceTypeAvailable(.Camera) {
imagePicker.sourceType = .Camera
}
else {
imagePicker.sourceType = .PhotoLibrary
}
imagePicker.delegate = self
presentViewController(imagePicker, animated: true, completion: nil)
}
So first, we create a function called takePicture(). We create a variable and assign it an instance of the UIImagePickerController class. Now, we can use that variable to get to the methods of the class.
First we check whether the device has a camera. We do so by calling the isSourceTypeAvailable method. If the device has a camera, it will pick the camera. Otherwise, it will choose from the library.
Step two is to set the delegate. Let the class, for example UIViewController, know by assigning the UIImagePickerControllerDelegate protocol. Your class will look something like this:
class viewController: UIViewController, UIImagePickerControllerDelegate {
…
}
We assign the delegate of the imagePicker to self.
It’s time to place the image on the screen. The method called presentViewController:animated:completion: is the one we are looking for.
This is a simple breakdown how to use the UIImagePickerController class, but there is more !
What we didn’t touch yet, is how to dismiss the Camera function. You will need this in order to have a positive user experience.
Let’s check below code:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: AnyObject]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
imageView.userInteractionEnabled = true
imageView.image = image
dismissViewControllerAnimated(true, completion: nil)
}
imagePickerController:didFinishPickingMediaWithInfo: is a delegate method, which you need in order to dismiss the camera after the picture is taken.
First we tell it that we created a picture (as UIImage). Now we can assign that image to the imageView. In the meantime, we can make it so that we can re-take a picture if the shown result is not something we like.
Last, but not least, the imagepicker will be removed from the screen with the dismiss method:
dismissViewControllerAnimated(true, completion: nil)
So far an introduction in the usage of UIImagePickerController, maybe in the future we will spice it up a notch with capturing video, customized camera controls or other cool stuff we can do with this class.