You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
7.4 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. //
  2. // MultiMediaManager.swift
  3. //
  4. //
  5. // Created by shishir sapkota
  6. //
  7. import UIKit
  8. import Foundation
  9. import AVFoundation
  10. import Photos
  11. //import RSKImageCropper
  12. @objc protocol MultiMediaManagerDelegate {
  13. @objc optional func cropSize() -> CGSize
  14. @objc optional func didFinishPickingWithImage(image: UIImage)
  15. func didFinishPickingWithError(error: String)
  16. @objc optional func didFinishPickingVideo(url: URL)
  17. }
  18. @objc protocol MultimediaPresenterProtocol {
  19. func presenting() -> UIViewController
  20. }
  21. class MultiMediaManager: NSObject {
  22. private let imagePicker = UIImagePickerController()
  23. private weak var presenter: MultimediaPresenterProtocol?
  24. weak var delegate: MultiMediaManagerDelegate?
  25. init(presenter: MultimediaPresenterProtocol) {
  26. self.presenter = presenter
  27. super.init()
  28. imagePicker.delegate = self
  29. imagePicker.allowsEditing = true
  30. }
  31. func openPicker(mode: UIImagePickerControllerCameraCaptureMode) {
  32. var presentationStyle: UIAlertControllerStyle = .actionSheet
  33. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad) {
  34. presentationStyle = .alert
  35. }
  36. //
  37. let alertController = UIAlertController(title: nil, message: "Gme would like to access your camera", preferredStyle: presentationStyle)
  38. let cameraAction = UIAlertAction(title: "camera_text".localized(), style: UIAlertActionStyle.default) { (action) in
  39. self.openCamera(mode: .photo)
  40. }
  41. let galleryAction = UIAlertAction(title: "gallery_text".localized(), style: UIAlertActionStyle.default) { (action) in
  42. self.showPhotoGallery(mode: mode)
  43. }
  44. let cancelAction = UIAlertAction(title: "cancel_text".localized(), style: .cancel, handler: nil)
  45. alertController.addAction(cameraAction)
  46. alertController.addAction(galleryAction)
  47. alertController.addAction(cancelAction)
  48. self.presentViewController(viewController: alertController, animated: true, completion: nil)
  49. }
  50. private func presentViewController(viewController: UIViewController, animated: Bool, completion: (() -> Void)?) {
  51. OperationQueue.main.addOperation {
  52. self.presenter?.presenting().present(viewController, animated: animated, completion: completion)
  53. }
  54. }
  55. func openCamera(mode: UIImagePickerControllerCameraCaptureMode) {
  56. func openPicker() {
  57. self.imagePicker.sourceType = UIImagePickerControllerSourceType.camera
  58. let mediaTypes = ["public.image"]
  59. self.imagePicker.mediaTypes = mediaTypes
  60. self.imagePicker.cameraCaptureMode = mode
  61. self.imagePicker.allowsEditing = false
  62. self.presentViewController(viewController: self.imagePicker, animated: true, completion: nil)
  63. }
  64. PermissionHelper.isAllowedToRecordVideo { (isAllowed) in
  65. if isAllowed {
  66. openPicker()
  67. }else {
  68. self.presenter?.presenting().alert(message: "camera_access_denied_text", okAction: {
  69. let url = URL(string: UIApplicationOpenSettingsURLString)!
  70. if #available(iOS 10.0, *) {
  71. UIApplication.shared.open(url, options: [:], completionHandler: nil)
  72. } else {
  73. // Fallback on earlier versions
  74. }
  75. })
  76. }
  77. }
  78. }
  79. private func showPhotoGallery(mode: UIImagePickerControllerCameraCaptureMode) {
  80. PermissionHelper.isAllowedToShowGallary { (isAllowed) in
  81. if isAllowed {
  82. self.imagePicker.allowsEditing = false
  83. self.imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
  84. let mediaTypes = mode == .video ? ["public.image", "public.movie"] : ["public.image"]
  85. self.imagePicker.mediaTypes = mediaTypes
  86. self.presentViewController(viewController: self.imagePicker, animated: true, completion: nil)
  87. }else {
  88. self.presenter?.presenting().alert(message: "galery_access_denied_text".localized(), okAction: {
  89. let url = URL(string: UIApplicationOpenSettingsURLString)!
  90. if #available(iOS 10.0, *) {
  91. UIApplication.shared.open(url, options: [:], completionHandler: nil)
  92. } else {
  93. // Fallback on earlier versions
  94. }
  95. })
  96. }
  97. }
  98. }
  99. func resizeImage(image: UIImage, to targetSize: CGSize) -> UIImage? {
  100. // This is the rect that we've calculated out and this is what is actually used below
  101. let rect = CGRect(origin: CGPoint.zero, size: targetSize)
  102. // Actually do the resizing to the rect using the ImageContext stuff
  103. UIGraphicsBeginImageContextWithOptions(targetSize, false, 1.0)
  104. image.draw(in: rect)
  105. let newImage = UIGraphicsGetImageFromCurrentImageContext()
  106. UIGraphicsEndImageContext()
  107. return newImage
  108. }
  109. }
  110. extension MultiMediaManager: UIImagePickerControllerDelegate {
  111. func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
  112. var completion: (()->())?
  113. if let mediaType = info["UIImagePickerControllerMediaType"] as? String {
  114. if mediaType == "public.image" {
  115. if let picture = info[UIImagePickerControllerOriginalImage] as? UIImage {
  116. if let cropsize = self.delegate?.cropSize?() {
  117. if picture.size.width < cropsize.width || picture.size.height < cropsize.height {
  118. self.delegate?.didFinishPickingWithError(error: "Image size must be greater than \(cropsize.width) X \(cropsize.height)")
  119. }else {
  120. guard let resizedImage = self.resizeImage(image: picture, to: cropsize) else { return }
  121. completion = {
  122. self.delegate?.didFinishPickingWithImage?(image: resizedImage)
  123. }
  124. // self.cropImage(image: picture)
  125. }
  126. }else {
  127. completion = {
  128. self.delegate?.didFinishPickingWithImage?(image: picture)
  129. }
  130. }
  131. }
  132. }else if mediaType == "public.movie" {
  133. _ = info["UIImagePickerControllerMediaURL"] as! URL
  134. return
  135. }
  136. }
  137. picker.dismiss(animated: true) {
  138. completion?()
  139. }
  140. }
  141. }
  142. extension MultiMediaManager: UINavigationControllerDelegate {
  143. }
  144. struct PermissionHelper {
  145. static func isAllowedToRecordVideo(completion: @escaping (Bool) -> ()) {
  146. let status = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
  147. completion(status != .denied && status != .restricted)
  148. }
  149. static func isAllowedToRecordSound(completion: @escaping (Bool) -> ()) {
  150. AVAudioSession.sharedInstance().requestRecordPermission(completion)
  151. }
  152. static func isAllowedToShowGallary(completion: @escaping (Bool) -> ()) {
  153. PHPhotoLibrary.requestAuthorization { (status) in
  154. completion(status == .authorized)
  155. }
  156. }
  157. }