Created
February 12, 2016 08:38
-
-
Save bizz84/53144c3f09a5e95d4d33 to your computer and use it in GitHub Desktop.
Simple Block-based wrapper around UIImagePickerController
This file contains 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
import UIKit | |
class MVPhotoPicker: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate { | |
// Class to hold strong reference to the currently presented photo picker controller | |
private class PhotoPickerController { | |
var currentPicker: MVPhotoPicker? | |
static let sharedInstance = PhotoPickerController() | |
} | |
private var completion: (image: UIImage?) -> () | |
private var picker: UIImagePickerController | |
// Will call completion block if the user has selected an image | |
// Will not call completion block if user dismissed the screen | |
class func choosePhoto(presentingVC: UIViewController, completion: (image: UIImage?) -> ()) { | |
let picker = MVPhotoPicker(completion: completion) | |
PhotoPickerController.sharedInstance.currentPicker = picker | |
presentingVC.presentViewController(picker.picker, animated: true, completion: nil) | |
} | |
private init(completion: (image: UIImage?) -> ()) { | |
self.completion = completion | |
picker = UIImagePickerController() | |
super.init() | |
picker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary | |
picker.delegate = self | |
} | |
// MARK: UIImagePickerControllerDelegate | |
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { | |
PhotoPickerController.sharedInstance.currentPicker = nil | |
var img = info[UIImagePickerControllerEditedImage] as? UIImage | |
if img == nil { | |
img = info[UIImagePickerControllerOriginalImage] as? UIImage | |
} | |
completion(image: img) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment