Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Protocol and extension for handling the presentation of image picker controllers and selection of media
// Since we are unable to extend ObjC protocols in swift, we wrap this around a custom NSObject subclass
// that deals exclusive with handling the image picker delegate methods and chaining the response down to
// the MediaPickerPresenter conforming delegate
class ImagePickerHandler: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var delegate: MediaPickerPresenter?
let sourceType: UIImagePickerControllerSourceType
let picker = UIImagePickerController()
init(sourceType: UIImagePickerControllerSourceType) {
self.sourceType = sourceType
super.init()
self.picker.delegate = self
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let mediaType = info[UIImagePickerControllerMediaType] as? String {
switch mediaType {
case String(kUTTypeImage):
if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
self.delegate?.didSelectFromMediaPicker(withImage: selectedImage)
}
case String(kUTTypeImage), String(kUTTypeMovie), String(kUTTypeVideo):
if let selectedMediaURL = info[UIImagePickerControllerMediaURL] as? NSURL {
self.delegate?.didSelectFromMediaPicker(withMediaUrl: selectedMediaURL)
}
default:
break
}
}
picker.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
}
protocol MediaPickerPresenter {
var imagePickerHandler: ImagePickerHandler { get set }
func presentMediaPicker(fromController controller: UIViewController, sourceType: UIImagePickerControllerSourceType)
func didSelectFromMediaPicker(withImage image: UIImage)
func didSelectFromMediaPicker(withMediaUrl mediaUrl: NSURL)
}
extension MediaPickerPresenter {
func presentMediaPicker(fromController controller: UIViewController, sourceType: UIImagePickerControllerSourceType) {
imagePickerHandler.delegate = self
controller.present(imagePickerHandler.picker, animated: true, completion: nil)
}
}
class PresenterViewController: UIViewController, MediaPickerPresenter {
var imagePickerHandler = ImagePickerHandler(sourceType: .photoLibrary)
func presentPicker() {
self.presentMediaPicker(fromController: self)
}
func didSelectFromMediaPicker(withImage image: UIImage) {
// ...
}
func didSelectFromMediaPicker(withMediaUrl mediaUrl: NSURL) {
// ...
}
}
@zjfjack

This comment has been minimized.

Copy link

zjfjack commented Mar 23, 2018

Thanks, a good solution to solve the problem.

@crgg

This comment has been minimized.

Copy link

crgg commented Dec 28, 2018

Hi, good solution, but don't assign type sourceType.

@experion-tibin

This comment has been minimized.

Copy link

experion-tibin commented Jan 1, 2019

Hi, good solution, but don't assign type sourceType.

Could you explain why?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.