Skip to content

Instantly share code, notes, and snippets.

@andrebian
Created September 24, 2015 20:15
Show Gist options
  • Save andrebian/73321f80f0e9371c8c6b to your computer and use it in GitHub Desktop.
Save andrebian/73321f80f0e9371c8c6b to your computer and use it in GitHub Desktop.

Revisions

  1. andrebian created this gist Sep 24, 2015.
    55 changes: 55 additions & 0 deletions CameraViewController.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    import UIKit
    import MobileCoreServices


    class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

    @IBOutlet weak var imgView: UIImageView!
    var newMedia: Bool?


    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    }

    @IBAction func takePicture(sender: AnyObject) {


    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {

    let picker = UIImagePickerController()
    picker.delegate = self
    picker.sourceType = UIImagePickerControllerSourceType.Camera
    picker.mediaTypes = [kUTTypeImage as String]
    picker.allowsEditing = false
    self.presentViewController(picker, animated: true, completion: nil)

    } else {

    NSLog("No Camera.")
    let alert = UIAlertController(title: "No camera", message: "Please allow this app the use of your camera in settings or buy a device that has a camera.", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default, handler: nil))
    self.presentViewController(alert, animated: true, completion: nil)
    }

    }


    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: NSDictionary, editingInfo: [String : AnyObject]?) {

    let temp: UIImage = info[UIImagePickerControllerOriginalImage] as! UIImage

    print("Exibindo a imagem tirada")
    print(temp)

    imgView.image = temp
    self.dismissViewControllerAnimated(true, completion: nil)
    }

    }