Created
October 9, 2017 05:35
-
-
Save NikhilManapure/47522256ce64a53cd75e8bc21f64e285 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
extension CameraViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate { | |
static let sourceType: UIImagePickerControllerSourceType = .photoLibrary | |
static let imagePickerController: UIImagePickerController? = { | |
if UIImagePickerController.isSourceTypeAvailable(sourceType) { | |
let imagePickerController = UIImagePickerController() | |
imagePickerController.sourceType = sourceType | |
return imagePickerController | |
} else { | |
return nil | |
} | |
}() | |
@IBAction func libraryButtonTouched(_ sender: UIButton) { | |
presentImagePicker(allowingEditing: false) | |
} | |
func presentImagePicker(allowingEditing: Bool) { | |
if let picker = CameraViewController.imagePickerController { | |
picker.delegate = self | |
self.present(picker, animated: true, completion: nil) | |
} | |
} | |
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { | |
var image: UIImage? | |
if info.keys.contains(UIImagePickerControllerEditedImage) { | |
image = info[UIImagePickerControllerEditedImage] as? UIImage | |
} else { | |
image = info[UIImagePickerControllerOriginalImage] as? UIImage | |
} | |
// Use `image` | |
self.dismiss(animated: true, completion: nil) | |
} | |
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { | |
self.dismiss(animated: true, completion: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment