Skip to content

Instantly share code, notes, and snippets.

@Appletone
Last active January 13, 2016 08:32
Show Gist options
  • Save Appletone/7a156a6348b90111037a to your computer and use it in GitHub Desktop.
Save Appletone/7a156a6348b90111037a to your computer and use it in GitHub Desktop.
Making UIViewController Image Pickerable
protocol ImagePickable {
var imagePicker:UIImagePickerController { get set }
func openPhotoLibrary()
}
var AssociatedObjectHandle: UInt8 = 0
extension UIViewController : ImagePickable, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var imagePicker:UIImagePickerController {
get {
return objc_getAssociatedObject(self, &AssociatedObjectHandle) as! UIImagePickerController
}
set {
objc_setAssociatedObject(self, &AssociatedObjectHandle, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
func openPhotoLibrary() {
imagePicker = UIImagePickerController()
imagePicker.modalPresentationStyle = .CurrentContext
imagePicker.sourceType = .PhotoLibrary
imagePicker.delegate = self
presentViewController(imagePicker, animated: true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment