6.6. Using Camera: UIImagePickerController
Now it's time to build the final part of the app: integrating the camera to take a photo.
Please note: the camera doesn't work in the emulator; you need a physical iOS device to test it. Do not worry; picking images using the camera is not mandatory in this course. This is an example for your future reference.
So, let's open AddExpenseViewController.swift file. We will write codes in pickUsingCamera() method. We also need to adopt UINavigationControllerDelegate, and UIImagePickerControllerDelegate protocols using the extension keyword. Let's add the following code in AddExpenseViewController.swift file:
//
// AddExpenseViewController.swift
// App6
//
// Created by Sakib Miazi on 5/18/23.
//
//codes omitted...
class AddExpenseViewController: UIViewController {
//codes omitted...
//MARK: variable to store the picked Image...
var pickedImage:UIImage?
//Codes omitted...
override func viewDidLoad() {
super.viewDidLoad()
//codes omitted...
//MARK: adding menu to buttonTakePhoto...
addExpenseScreen.buttonTakePhoto.menu = getMenuImagePicker()
//codes omitted...
}
//codes omitted...
func getMenuImagePicker() -> UIMenu{
var menuItems = [
UIAction(title: "Camera",handler: {(_) in
self.pickUsingCamera()
}),
UIAction(title: "Gallery",handler: {(_) in
self.pickPhotoFromGallery()
})
]
return UIMenu(title: "Select source", children: menuItems)
}
//codes omitted...
//MARK: take Photo using Camera...
func pickUsingCamera(){
let cameraController = UIImagePickerController()
cameraController.sourceType = .camera
cameraController.allowsEditing = true
cameraController.delegate = self
present(cameraController, animated: true)
}
//codes omitted...
}
//codes omitted...
//MARK: adopting required protocols for UIImagePicker...
extension AddExpenseViewController: UINavigationControllerDelegate, UIImagePickerControllerDelegate{
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picker.dismiss(animated: true)
if let image = info[.editedImage] as? UIImage{
self.addExpenseScreen.buttonTakePhoto.setImage(
image.withRenderingMode(.alwaysOriginal),
for: .normal
)
self.pickedImage = image
}else{
// Do your thing for No image loaded...
}
}
}Here we can see using a camera is pretty straightforward.
Inside pickUsingCamera() method:
We create an instance of
UIImagePickerControllernamedcameraController.Then we set the source type of the controller to the camera. (This controller was also used to pick gallery images; however, Apple deprecated sources other than the camera for this controller).
Then we allow cropping and editing after the user takes the photo.
We set the camera delegate to
selfto handle the images after they are picked.Then we present the camera.
Adopting UINavigationControllerDelegate and UIImagePickerControllerDelegate protocol:
We must implement the
imagePickerController()method corresponding todidFinishPickingMediaWithInfo.First, we must dismiss the picker; otherwise, iOS will not close the camera app.
Then we unwrap the image we took and edited.
Finally, we set it to
pickedImage.
Changing the privacy settings of the App to allow the app to access the Camera
You need to update the privacy description for Camera Usage in the app 'Info' configurations.
Click on 'Info' from the left pane.
Right-click on the empty space
Select "Add Row."
Find "Privacy - Camera Usage Description."
Add "We need to use the camera to take the receipt of the expense." as your justification for the user to access the camera.

Now let's run the app. I ran the app on my iPad to demonstrate it is working.
Last updated
Was this helpful?