Created
March 18, 2017 06:25
-
-
Save abhimuralidharan/9490ae1178a4bdb60f6d8b3a8cbfb2fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // ViewController.swift | |
| // ImagePicker | |
| // | |
| // Created by Abhilash on 15/03/17. | |
| // Copyright © 2017 Abhilash. All rights reserved. | |
| // | |
| import UIKit | |
| import MobileCoreServices | |
| import QuartzCore | |
| class ViewController: UIViewController { | |
| let imagePicker = UIImagePickerController() | |
| @IBOutlet weak var imageView: UIImageView! | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| // Do any additional setup after loading the view, typically from a nib. | |
| } | |
| override func viewDidAppear(_ animated: Bool) { | |
| imageView.layer.cornerRadius = imageView.frame.size.width/2 | |
| imageView.layer.masksToBounds = true | |
| } | |
| @IBAction func openCamAction(_ sender: Any) { | |
| openCamera() | |
| } | |
| func openCamera() { | |
| guard UIImagePickerController.isSourceTypeAvailable(.camera) else { | |
| print("This device doesn't have a camera.") | |
| return | |
| } | |
| imagePicker.sourceType = .camera | |
| imagePicker.cameraDevice = .rear | |
| // imagePicker.mediaTypes = [kUTTypeImage as String] | |
| imagePicker.mediaTypes = UIImagePickerController.availableMediaTypes(for:.camera)! | |
| imagePicker.delegate = self | |
| present(imagePicker, animated: true) | |
| } | |
| @IBAction func openLibraryAction(_ sender: Any) { | |
| openPhotoLibrary() | |
| } | |
| func openPhotoLibrary() { | |
| guard UIImagePickerController.isSourceTypeAvailable(.photoLibrary) else { | |
| print("can't open photo library") | |
| return | |
| } | |
| imagePicker.sourceType = .photoLibrary | |
| imagePicker.delegate = self | |
| present(imagePicker, animated: true) | |
| } | |
| } | |
| extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate { | |
| func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { | |
| defer { | |
| picker.dismiss(animated: true) | |
| } | |
| print(info) | |
| // get the image | |
| guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else { | |
| return | |
| } | |
| // do something with it | |
| imageView.image = image | |
| } | |
| func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { | |
| defer { | |
| picker.dismiss(animated: true) | |
| } | |
| print("did cancel") | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment