Accessing iOS Camera and Photo Library is maybe one of the most common features that you can find in almost every app that we build. That’s why we have to make sure that we do it correctly and that we have a custom class ready to be reused at any time.
In this tutorial, I will show you how to create the custom class in Swift, and have it by your hand whenever you need it. If you are lazy to read the whole tutorial, you have a GIST file at the bottom of this post that you can download. I will name the class CameraHandler.swift.
I will start by creating two functions, the first will be named camera() and the second one photoLibrary(). We are using the UIImagePickerController class for both cases, and all we need to do is to change the sourceType property to the suitable one.
func camera() { if UIImagePickerController.isSourceTypeAvailable(.camera){ let myPickerController = UIImagePickerController() myPickerController.delegate = self; myPickerController.sourceType = .camera currentVC.present(myPickerController, animated: true, completion: nil) } }
func photoLibrary() { if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){ let myPickerController = UIImagePickerController() myPickerController.delegate = self; myPickerController.sourceType = .photoLibrary currentVC.present(myPickerController, animated: true, completion: nil) } }
As you can see, the sourceType changes to .camera and .photoLibrary types. We will need the UIImagePickerControllerDelegate and UINavigationControllerDelegate so we intercept the image that is picked by the user. I will explain below about the currentVC property.
Next, what we are going to do is create a function that will present both options to the user in a simple UIActionSheet.
func showActionSheet(vc: UIViewController) { currentVC = vc let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (alert:UIAlertAction!) -> Void in self.camera() })) actionSheet.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { (alert:UIAlertAction!) -> Void in self.photoLibrary() })) actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) vc.present(actionSheet, animated: true, completion: nil) }
This is the only function that you will need to call in order to show the iOS Camera and Photo Library. It is showing a simple UIActionSheet with the both options available. Also, we are passing a parameter called vcthat will be passed to the currentVC private property. We are doing this in order to handle the presentation of the controllers directly from inside the class.
NOTE: If the device doesn’t supports camera or photo library, nothing will happen when you press an option. For example, testing the camera feature on simulator.
At the end of the file in we will create an extension, and we will call the delegate methods didFinishPickingMediaInfo and imagePickerControllerDidCancel that belongs to the UIImagePickerControllerDelegate.
extension CameraHandler: UIImagePickerControllerDelegate, UINavigationControllerDelegate{ func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { currentVC.dismiss(animated: true, completion: nil) } func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { if let image = info[UIImagePickerControllerOriginalImage] as? UIImage { self.imagePickedBlock?(image) }else{ print("Something went wrong") } currentVC.dismiss(animated: true, completion: nil) }}
We are now able to get the picked image from the photo library or the captured one from the camera. To make things simpler and clearer, I have created a closure named imagePickedBlock() that will provide us with the picked image where we need it. Here are all the properties that you will need.
static let shared = CameraHandler() fileprivate var currentVC: UIViewController! //MARK: Internal Propertiesvar imagePickedBlock: ((UIImage) -> Void)?
We are done creating the class now we need to use it. The beauty in classes like this one is the easy reuse.
CameraHandler.shared.showActionSheet(vc: self)CameraHandler.shared.imagePickedBlock = { (image) in /* get your image here */}
That’s it from this tutorial that showed you how to access iOS Camera and Photo Library in Swift 3, and I really hope that it helped you. Please do share this post as a support or comment in the comment section for any questions that you might have.
1x2 BET - Soccer Tips & Odds_HOT ODDS Each day, we generate a list of the hottest odds in the world. These are odds that have dropped the most…_apple.co
Introducing Clean Swift Architecture (VIP)_Forget MVC, now!_hackernoon.com
Your ultimate guide to the Google Maps SDK on iOS, using Swift 4_Many iOS apps use Google Maps. This is a very common feature, so I have decided to prepare an ultimate guide on the…_medium.freecodecamp.org
SWIFT — Custom UIView with XIB file_Custom UIView with XIB file is a very common practice in iOS Development. Custom UIView classes don’t contain XIB files…_medium.com
How to add Spotlight support to your iOS app_A Swift tutorial that will make your app available in Spotlight search_hackernoon.com
Core Data Relationships_Understanding One-to-One and One-To-Many relationships_hackernoon.com
Understanding Auto Layout in Xcode 9_All you need to know about Auto Layout_hackernoon.com