Advertisement
Guest User

Untitled

a guest
May 27th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. //
  2. // UIViewController+GetImage.swift
  3. //
  4. // Created by Daniel Tartaglia on 4/25/16.
  5. // Copyright © 2016 MIT License
  6. //
  7.  
  8. import UIKit
  9. import PromiseKit
  10.  
  11.  
  12. enum ImagePickerError: ErrorType {
  13. case UserCanceled
  14. }
  15.  
  16. extension UIViewController {
  17.  
  18. func getImage(focusView view: UIView) -> Promise<UIImage> {
  19. let proxy = ImagePickerProxy()
  20. let cameraAction: UIAlertAction? = !UIImagePickerController.isSourceTypeAvailable(.Camera) ? nil : UIAlertAction(title: "Camera", style: .Default) { _ in
  21. let controller = UIImagePickerController()
  22. controller.delegate = proxy
  23. controller.allowsEditing = true
  24. controller.sourceType = .Camera
  25. self.presentViewController(controller, animated: true, completion: nil)
  26. }
  27. let photobinAction: UIAlertAction? = !UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary) ? nil : UIAlertAction(title: "Photos", style: .Default) { _ in
  28. let controller = UIImagePickerController()
  29. controller.delegate = proxy
  30. controller.allowsEditing = false
  31. controller.sourceType = .PhotoLibrary
  32. self.presentViewController(controller, animated: true, completion: nil)
  33. }
  34. let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
  35.  
  36. let alert = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
  37. if let cameraAction = cameraAction {
  38. alert.addAction(cameraAction)
  39. }
  40. if let photobinAction = photobinAction {
  41. alert.addAction(photobinAction)
  42. }
  43. alert.addAction(cancelAction)
  44. let popoverPresentationController = alert.popoverPresentationController
  45. popoverPresentationController?.sourceView = view
  46. popoverPresentationController?.sourceRect = view.bounds
  47. presentViewController(alert, animated: true, completion: nil)
  48. let promise = proxy.promise
  49. return promise.always {
  50. self.dismissViewControllerAnimated(true, completion: nil)
  51. proxy.retainCycle = nil
  52. }
  53. }
  54. }
  55.  
  56.  
  57. private final class ImagePickerProxy: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
  58.  
  59. let (promise, fulfill, reject) = Promise<UIImage>.pendingPromise()
  60. var retainCycle: ImagePickerProxy?
  61.  
  62. required override init() {
  63. super.init()
  64. retainCycle = self
  65. }
  66.  
  67. @objc func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
  68. let image = (info[UIImagePickerControllerEditedImage] as? UIImage) ?? (info[UIImagePickerControllerOriginalImage] as! UIImage)
  69. fulfill(image)
  70. }
  71.  
  72. @objc func imagePickerControllerDidCancel(picker: UIImagePickerController) {
  73. reject(ImagePickerError.UserCanceled)
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement