0

I am starting from zero with iOS app development but I need to create an app that can use the iPhone camera to take an image. I have found UIImagePickerController but I cannot figure out how to implement it correctly. If anyone could help me that would be great. Also I know very little about app development so if there is any background knowledge I would need to understand your answers would be appreciated.

Asperi
  • 228,894
  • 20
  • 464
  • 690

1 Answers1

1

You can call this method wherever you want to open UIImagePickerController:

func openImagePicker() {
    let vc = UIImagePickerController()
    vc.sourceType = .camera
    vc.allowsEditing = true
    vc.delegate = self
    present(vc, animated: true)
}

Make your view controller conform to both UINavigationControllerDelegate and UIImagePickerControllerDelegate by making an extension like this:

extension youViewControllerClass: UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        
        picker.dismiss(animated: true) // dismisses camera controller

        guard let image = info[.editedImage] as? UIImage else {
            print("No image found")
            return
        }

        // You will get your image here
        print(image.size)
     }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController){
        picker.dismiss(animated: true, completion: nil)
        // here you will get the cancel event
    }

}
Chitra
  • 46
  • 5
  • Thank you. I am attempting to store the image on the app's cache so it can be analyzed without storing it on the user's device (security reasons). Do you have any ideas on how to do this? One more question. When I try to put this in the view where I will open the camera, I get an error for the `vc.delegate = self` as well as the `present(vc, animated: true)` lines. Both of the errors say that I am using an unresolved identifier for self and present. I also get an error at the `extension youViewControllerClass:...` line. This error says that I am using an undeclared type. Any ideas to fix this? – AGoraya0710 Jul 01 '20 at 18:01
  • @AGoraya0710 You have to put this code in your UIViewController class where you have your view because `present(vc, animated: true)` is the property of UIViewController and its subclasses. Also you need to replace `youViewControllerClass` to original name of your UIViewController. – Chitra Jul 02 '20 at 05:56
  • I apologize if this is a stupid question. I do not have a UIViewController Class. The classes I have are: PhotoCaptureView.swift, ImagePicker.swift, ContentView.swift, SceneDelegate.swift, and AppDelegate.swift. I can link images of the code inside each if you would like, but I don't currently have a UIViewController class. Where should I create this class and how? – AGoraya0710 Jul 02 '20 at 16:17
  • you can check this answer if you are using swiftUI, i hope that will help. [https://stackoverflow.com/questions/56515871/how-to-open-the-imagepicker-in-swiftui] – Chitra Jul 03 '20 at 05:11