Advertisement
Guest User

Untitled

a guest
Jun 30th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. //
  2. // CameraHandler.swift
  3. // theappspace.com
  4. //
  5. // Created by Dejan Atanasov on 26/06/2017.
  6. // Copyright © 2017 Dejan Atanasov. All rights reserved.
  7. //
  8.  
  9. import Foundation
  10. import UIKit
  11.  
  12.  
  13. class CameraHandler: NSObject{
  14. static let shared = CameraHandler()
  15.  
  16. fileprivate var currentVC: UIViewController!
  17.  
  18. //MARK: Internal Properties
  19. var imagePickedBlock: ((UIImage) -> Void)?
  20.  
  21. func camera()
  22. {
  23. if UIImagePickerController.isSourceTypeAvailable(.camera){
  24. let myPickerController = UIImagePickerController()
  25. myPickerController.delegate = self;
  26. myPickerController.sourceType = .camera
  27. currentVC.present(myPickerController, animated: true, completion: nil)
  28. }
  29.  
  30. }
  31.  
  32. func photoLibrary()
  33. {
  34.  
  35. if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
  36. let myPickerController = UIImagePickerController()
  37. myPickerController.delegate = self;
  38. myPickerController.sourceType = .photoLibrary
  39. currentVC.present(myPickerController, animated: true, completion: nil)
  40. }
  41.  
  42. }
  43.  
  44. func showActionSheet(vc: UIViewController) {
  45. currentVC = vc
  46. let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
  47.  
  48. actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (alert:UIAlertAction!) -> Void in
  49. self.camera()
  50. }))
  51.  
  52. actionSheet.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { (alert:UIAlertAction!) -> Void in
  53. self.photoLibrary()
  54. }))
  55.  
  56. actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
  57.  
  58. vc.present(actionSheet, animated: true, completion: nil)
  59. }
  60.  
  61. }
  62.  
  63.  
  64. extension CameraHandler: UIImagePickerControllerDelegate, UINavigationControllerDelegate{
  65. func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
  66. currentVC.dismiss(animated: true, completion: nil)
  67. }
  68.  
  69. func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
  70. if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
  71. self.imagePickedBlock?(image)
  72. }else{
  73. print("Something went wrong")
  74. }
  75. currentVC.dismiss(animated: true, completion: nil)
  76. }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement