Clicking or uploading pictures in iOS
In this blog, we will learn how we can set our iOS app to enable camera to click pictures or upload picture from the photo library.
Following are the steps to achieve this:
- Add these two keys in your
info.plist
so that your app can open the Camera & Photo library.
2. Now that we have permission to access the camera and photo library, let’s write the code that will open the above actionSheet for giving the option from where user can select where to take photo
func presentActionSheet() {
let actionSheet = UIAlertController(title: "Profile Picture",
message: "How would you like to select a picture",
preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
actionSheet.addAction(UIAlertAction(title: "Take Photo",
style: .default,
handler: { [weak self] _ in
self?.presentCamera()
}))
actionSheet.addAction(UIAlertAction(title: "Choose Photo",
style: .default,
handler: { [weak self] _ in
self?.presentPhotoLibrary()
}))
present(actionSheet, animated: true)
}
3. In the above method let’s write presentCamera
method
func presentCamera() {
let vc = UIImagePickerController()
vc.sourceType = .camera
vc.allowsEditing = true
vc.delegate = self
present(vc, animated: true)
}
Basically, in this method we are just creating an instance of UIImagePickerController
and then presenting it with sourceType .camera
so it will open and ask for access to the camera from user
4. Now, let’s write a method to open the photo gallery presentPhotoLibrary
func presentPhotoLibrary() {
let vc = UIImagePickerController()
vc.sourceType = .photoLibrary
vc.allowsEditing = true
vc.delegate = self
present(vc, animated: true)
}
In this method, we have just changed the sourceType to .photoLibrary
4. You have to confirm your controller with UIImagePickerControllerDelegate
and UINavigationControllerDelegate
which is going to tell you that the user has cancelled the photo upload or the upload is finished.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let selectedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage else {
return
}
picker.dismiss(animated: true, completion: nil)
imageView.image = selectedImage
/// Whatever you want to do here with the image or the
/// image properties we got above you can do here.
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
As we have done allowsEditing
true in our UIImagePickerController above, we are getting editingImage
from the info we got from the delegate
let selectedImage = info[UIImagePickerController.InfoKey.editedImage]
Here is the final code blocks used above:
extension RegisterViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func presentActionSheet() {
let actionSheet = UIAlertController(title: "Profile Picture",
message: "How would you like to select a picture",
preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
actionSheet.addAction(UIAlertAction(title: "Take Photo",
style: .default,
handler: { [weak self] _ in
self?.presentCamera()
}))
actionSheet.addAction(UIAlertAction(title: "Choose Photo",
style: .default,
handler: { [weak self] _ in
self?.presentPhotoLibrary()
}))
present(actionSheet, animated: true)
}
func presentCamera() {
let vc = UIImagePickerController()
vc.sourceType = .camera
vc.allowsEditing = true
vc.delegate = self
present(vc, animated: true)
}
func presentPhotoLibrary() {
let vc = UIImagePickerController()
vc.sourceType = .photoLibrary
vc.allowsEditing = true
vc.delegate = self
present(vc, animated: true)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let selectedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage else {
return
}
picker.dismiss(animated: true, completion: nil)
imageView.image = selectedImage
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
}
Thank you for reading!
Please leave comments if you have any suggestion(s) or would like to add point(s) or if you noticed any mistakes or typos or any queries!
P.S. If you found this article helpful, clap! 👏👏👏 [feels rewarding and gives the motivation to continue my writing].