ios Camera app swift 3

604 views Asked by At

I want a camera interface in my app with capture and save image capabilities but i'm not able to get anything when i run this app, build is successful without any errors but nothing is showing on the viewController.

class ViewController: UIViewController , UINavigationControllerDelegate, UIImagePickerControllerDelegate{

@IBOutlet var imageView: UIImageView!


@IBOutlet var TakePhoto: UIButton!
var imagePick: UIImagePickerController!
var newMedia: Bool?

@IBAction func TakePhoto(_ sender: AnyObject){

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){
        let imagePick = UIImagePickerController()
        imagePick.delegate = self
        imagePick.sourceType = UIImagePickerControllerSourceType.camera
        imagePick.mediaTypes = [kUTTypeImage as String]
        imagePick.allowsEditing = false

        self.present(imagePick, animated: true, completion: nil)

        newMedia = true

    }

}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let mediaType = info[UIImagePickerControllerMediaType] as! NSString
    if mediaType.isEqual(to: kUTTypeImage as String){
        let image = info[UIImagePickerControllerOriginalImage] as! UIImage
        imageView.image = image

        if(newMedia == true){
           UIImageWriteToSavedPhotosAlbum(image, self, #selector(ViewController.image(image:didFinishSavingWithError:contextInfo:)), nil)
        } //else if mediaType.isEqual(to: kUTTypeMovie as String)

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

    }
}

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

        let cancelAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)

        alert.addAction(cancelAction)
        self.present(alert, animated: true, completion: nil)
    }
}

}

1

There are 1 answers

2
ErgesaC On

Have you set the permission to use camera in info.plist?

I mean to put the key Privacy - Camera Usage Description, type String with a description why are you using it