0

Im trying to learn how to access and use the camera on IOS devices, trufully i cant find all that much info on it, at least not thats up to date or useful to learn at a beginner level.

Regardless, I've tried the below code but nothing happens when I click the button to open the camera it just freezes and does nothing, am i missing something or have i done something wrong?

import UIKit
import MobileCoreServices

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    @IBOutlet weak var myImg: UIImageView!
    var newMedia: Bool!

    @IBAction func cameraRoll(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.savedPhotosAlbum){
            let imagePicker = UIImagePickerController()

            imagePicker.delegate = self
            imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
            imagePicker.mediaTypes = [kUTTypeImage as String]
            imagePicker.allowsEditing = false
            self.present(imagePicker, animated: true, completion: nil)
            newMedia = false
        }
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        let mediaType = info[UIImagePickerControllerMediaType] as! NSString

        self.dismiss(animated: true, completion: nil)

        if mediaType.isEqual(to: kUTTypeImage as String){
            let image = info[UIImagePickerControllerOriginalImage] as! UIImage

            myImg.image = image

            if (newMedia == true) {
                UIImageWriteToSavedPhotosAlbum(image, self, #selector(ViewController.image(image:didFinishsavingWithError:contextInfo:)), nil)

            } else if mediaType.isEqual(to: kUTTypeMovie as String){
                //vid support
            }
        }
    }

    func image(image: UIImage, didFinishsavingWithError error: NSErrorPointer, contextInfo:UnsafeRawPointer){
        if error != nil {
            let alert = UIAlertController(title: "Save Failed", message: "Failed to save image", preferredStyle: UIAlertControllerStyle.alert)

            let cancelAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
            alert.addAction(cancelAction)
            self.present(alert, animated: true, completion: nil)
        }
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        self.dismiss(animated: true, completion: nil)
    }

    @IBAction func takePhoto(_ sender: Any) {

        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){

            let imagePicker = UIImagePickerController()

            imagePicker.delegate = self
            imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
            imagePicker.mediaTypes = [kUTTypeImage as String]
            imagePicker.allowsEditing = false
            self.present(imagePicker, animated: true, completion:  nil)
            newMedia = false
        }     
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
}
mugx
  • 9,869
  • 3
  • 43
  • 55
Mick
  • 71
  • 7
  • For one thing in your `takePhoto` method, which I assume you want to use to access your camera, you're setting the imagePicker source to `.photoLibrary`. Also have you implemented the security protocols by adding `NSCameraUsageDescription` and `NSPhotoLibraryUsageDescription` keys plus an explanation for each? If not it will probably cause a crash – Pierce Feb 05 '17 at 03:57
  • What's there is what I've done, i was following a tutorial – Mick Feb 05 '17 at 04:05
  • Your are checking for source type camera is available or not and then in code below you are setting sourceType as photoLibrary, change it to UIImagePickerControllerSourceType.Camera. And one more thing check permissions of camera for your app in settings. – Tejas Shelke Feb 05 '17 at 06:00
  • Are you trying it on simulator or actual device? Because functionality related camera won't work on simulator. – Tejas Shelke Feb 05 '17 at 06:04
  • 1
    Possible duplicate http://stackoverflow.com/questions/41100717/access-to-camera-and-photolibrary/41102098#41102098 – Joe Feb 05 '17 at 08:13
  • im testing on an actual device. Thanks Joe, ill check that out. – Mick Feb 06 '17 at 02:12
  • Let me know if that linked question helped you. If it did, I will close your question as a duplicate. – JAL Feb 08 '17 at 04:05
  • Yeah that link helped, thankyou – Mick Feb 14 '17 at 00:15

0 Answers0