Last active
January 13, 2016 08:32
-
-
Save Appletone/7a156a6348b90111037a to your computer and use it in GitHub Desktop.
Making UIViewController Image Pickerable
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
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