Skip to content

Instantly share code, notes, and snippets.

@KiranSarella
Created April 15, 2020 07:35
Show Gist options
  • Save KiranSarella/46d2d1123e24adf353fb0232d0d68201 to your computer and use it in GitHub Desktop.
Save KiranSarella/46d2d1123e24adf353fb0232d0d68201 to your computer and use it in GitHub Desktop.
UIImagePickerController helper to pick image from camera/gallery
import UIKit
class ImagePickerHelper: NSObject {
var imagePicker = UIImagePickerController()
var handleDidFinishPickingImage: ((UIImage) -> ())?
unowned var source: UIViewController!
func showCameraOptions(on viewController: UIViewController) {
source = viewController
imagePicker.delegate = self
viewController.loadAlertForImageSelection(imagePicker: imagePicker)
}
}
extension ImagePickerHelper : UIImagePickerControllerDelegate,UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let image = info[.originalImage] as! UIImage
handleDidFinishPickingImage?(image)
DispatchQueue.main.async {
self.source.dismiss(animated: true, completion: nil)
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
DispatchQueue.main.async {
picker.dismiss(animated: true, completion: nil)
}
}
}
extension UIViewController {
func checkCameraAvailability(imagePicker : UIImagePickerController) {
if UIImagePickerController.isSourceTypeAvailable(.camera) {
imagePicker.sourceType = .camera
imagePicker.cameraCaptureMode = .photo
// imagePicker.mediaTypes = [kUTTypeImage as String]
imagePicker.modalPresentationStyle = .fullScreen
present(imagePicker, animated: true, completion: nil)
} else {
let alertController = UIAlertController(title: "", message: "camara.notSupported.message".localized(), preferredStyle: .alert)
let okButton = UIAlertAction(title: "ok", style: .default) { (action) in
alertController.dismiss(animated: true, completion: nil)
}
alertController.addAction(okButton)
present(alertController, animated: true, completion: nil)
}
}
func loadAlertForImageSelection(imagePicker : UIImagePickerController) {
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let photoBtn = UIAlertAction(title: "Take a photo", style: .default) { (action) in
self.checkCameraAvailability(imagePicker: imagePicker)
}
let choosePhotoBtn = UIAlertAction(title: "Choose from library", style: .default) { (action) in
imagePicker.sourceType = .photoLibrary
// self.imagePickerCotroller.mediaTypes = [kUTTypeImage] as [String]
self.present(imagePicker, animated: true, completion: nil)
}
let cancelBtn = UIAlertAction(title: "cancel.title".localized(), style: .cancel) { (action) in
alertController.dismiss(animated: true, completion: nil)
}
alertController.addAction(photoBtn)
alertController.addAction(choosePhotoBtn)
alertController.addAction(cancelBtn)
self.present(alertController, animated: true, completion: nil)
}
}
@KiranSarella
Copy link
Author

Usage:

lazy var imagePickerHelper = ImagePickerHelper()

@IBAction func addPhotoAction(_ sender: Any) {

    imagePickerHelper.handleDidFinishPickingImage = { image in
        print(image)
        self.profileImageView.image = image
    }
    
    imagePickerHelper.showCameraOptions(on: self)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment