How to access the Camera and Photos in Swift.
What you will learn
- How to access the user’s camera and take a photo
- How to then get this photo and use it in your app
- How to save a photo to the users photos
- How to access the user’s photos and do the same
The project
We have setup an Xcode project as follows.
Connect the image and buttons as follows to your view controller:
Imageview as an outlet named “myImg”
Take Photo Button as an action named “takePhoto”
Save Photo Button as an action named “savePhoto”
PIck Photo Button as an action named “pickPhoto”
Once this is done also add UIImagePickerControllerDelegate and UINavigationController delegate to your class as follows:
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { ... }
This is needed to use functions such as an image picker and navigation controller that is used when taking & accessing photos in iOS.
Taking a photo with the users camera
To take a photo with the user’s camera add the following code to takePhoto
@IBAction func takePhoto(_ sender: AnyObject) { if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) { let imagePicker = UIImagePickerController() imagePicker.delegate = self imagePicker.sourceType = UIImagePickerControllerSourceType.camera imagePicker.allowsEditing = false self.present(imagePicker, animated: true, completion: nil) } }
What the code does is:
- Check if we have access to the camera (If not ask permission for access0
- Creates a new imagePicker controller which will use the users camera as a source
- Show the imagePicker controller to the user so they can take a photo.
This code will let you access the user’s camera, note to test it you will need to run it on an actually iOS device and not the simulator. This is as the simulator does not have a camera so it will crash as a result when you try and run this code!
Getting the photo you just took
So now you have taken a photo, that’s great! But where did it go, how did we get access to it?
To do this add the following method which will automatically be run whenever you take a photo from the camera or pick one from the user’s photo:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage { myImg.contentMode = .scaleToFill myImg.image = pickedImage } picker.dismiss(animated: true, completion: nil) }
What the code does is:
- Check if the info parameter contains an image
- If it does this is our image, then set our myImg image view to contain this image
- Hides the Camera/Photo Picker once the photo has been chosen
Now when you run the app and take a photo you will see the photo you took in the app on the image view we placed at the start! So now you have the photo here you can do whatever you wish with it.
Saving the photo back to the user’s photos
To save the photo to the camera roll add the following code to savePhoto
@IBAction func savePhoto(_ sender: AnyObject) { let imageData = UIImagePNGRepresentation(myImg.image!) let compresedImage = UIImage(data: imageData!) UIImageWriteToSavedPhotosAlbum(compresedImage!, nil, nil, nil) let alert = UIAlertController(title: "Saved", message: "Your image has been saved", preferredStyle: .alert) let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil) alert.addAction(okAction) self.present(alert, animated: true, completion: nil) }
What the code does is:
- Get the image content as a PNG as this makes the file size smaller
- Saves the image to the user’s photos album
- Present an alert to the user notifying them the image has been saved
Getting a photo from the users photos gallery
To get a photo from the users gallery add the following code to pickPhoto
@IBAction func pickPhoto(_ sender: AnyObject) { if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) { let imagePicker = UIImagePickerController() imagePicker.delegate = self imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary imagePicker.allowsEditing = true self.present(imagePicker, animated: true, completion: nil) } }
This code is almost the same as take photo, except the source is the user’s photos instead of the camera. Once you pick a photo from here the imagePIckerController didFinishPickingMediaWithInfo function will still be run so the imageview will still get set.
Unlike taking a photo you can test the code from picking from the photos gallery in the simulator.