Skip to content

Instantly share code, notes, and snippets.

@stepheng
Created January 3, 2016 14:40
Show Gist options
  • Save stepheng/1562ca7834ee84f1a64a to your computer and use it in GitHub Desktop.
Save stepheng/1562ca7834ee84f1a64a to your computer and use it in GitHub Desktop.
//
// CameraViewController.swift
// Camera
//
// Created by Stephen Gurnett on 03/01/2016.
// Copyright © 2016 Stephen Gurnett. All rights reserved.
//
import UIKit
import MobileCoreServices
class CameraViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
@IBAction func displayCamera() {
displayImagePicker(.Camera)
}
@IBAction func savedPhotos() {
displayImagePicker(.SavedPhotosAlbum)
}
@IBAction func photosLibrary() {
displayImagePicker(.PhotoLibrary)
}
func displayImagePicker(sourceType: UIImagePickerControllerSourceType) {
guard UIImagePickerController.isSourceTypeAvailable(sourceType) else {
return
}
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = sourceType
imagePicker.allowsEditing = false
imagePicker.mediaTypes = [kUTTypeImage as String]
presentViewController(imagePicker, animated: true, completion: nil)
}
func handleImage(image: UIImage) {
imageView.image = image
}
}
extension CameraViewController: UIImagePickerControllerDelegate {
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
dismissViewControllerAnimated(true, completion: nil)
let mediaType = info[UIImagePickerControllerMediaType] as! String
if mediaType == kUTTypeImage as String {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
handleImage(image)
}
}
}
extension CameraViewController: UINavigationControllerDelegate {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment