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.

175 lines
7.0 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. UIApplication.shared.open(url, options: [:], completionHandler: nil)
  71. })
  72. }
  73. }
  74. }
  75. private func showPhotoGallery(mode: UIImagePickerControllerCameraCaptureMode) {
  76. PermissionHelper.isAllowedToShowGallary { (isAllowed) in
  77. if isAllowed {
  78. self.imagePicker.allowsEditing = false
  79. self.imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
  80. let mediaTypes = mode == .video ? ["public.image", "public.movie"] : ["public.image"]
  81. self.imagePicker.mediaTypes = mediaTypes
  82. self.presentViewController(viewController: self.imagePicker, animated: true, completion: nil)
  83. }else {
  84. self.presenter?.presenting().alert(message: "galery_access_denied_text".localized(), okAction: {
  85. let url = URL(string: UIApplicationOpenSettingsURLString)!
  86. UIApplication.shared.open(url, options: [:], completionHandler: nil)
  87. })
  88. }
  89. }
  90. }
  91. func resizeImage(image: UIImage, to targetSize: CGSize) -> UIImage? {
  92. // This is the rect that we've calculated out and this is what is actually used below
  93. let rect = CGRect(origin: CGPoint.zero, size: targetSize)
  94. // Actually do the resizing to the rect using the ImageContext stuff
  95. UIGraphicsBeginImageContextWithOptions(targetSize, false, 1.0)
  96. image.draw(in: rect)
  97. let newImage = UIGraphicsGetImageFromCurrentImageContext()
  98. UIGraphicsEndImageContext()
  99. return newImage
  100. }
  101. }
  102. extension MultiMediaManager: UIImagePickerControllerDelegate {
  103. func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
  104. var completion: (()->())?
  105. if let mediaType = info["UIImagePickerControllerMediaType"] as? String {
  106. if mediaType == "public.image" {
  107. if let picture = info[UIImagePickerControllerOriginalImage] as? UIImage {
  108. if let cropsize = self.delegate?.cropSize?() {
  109. if picture.size.width < cropsize.width || picture.size.height < cropsize.height {
  110. self.delegate?.didFinishPickingWithError(error: "Image size must be greater than \(cropsize.width) X \(cropsize.height)")
  111. }else {
  112. guard let resizedImage = self.resizeImage(image: picture, to: cropsize) else { return }
  113. completion = {
  114. self.delegate?.didFinishPickingWithImage?(image: resizedImage)
  115. }
  116. // self.cropImage(image: picture)
  117. }
  118. }else {
  119. completion = {
  120. self.delegate?.didFinishPickingWithImage?(image: picture)
  121. }
  122. }
  123. }
  124. }else if mediaType == "public.movie" {
  125. _ = info["UIImagePickerControllerMediaURL"] as! URL
  126. return
  127. }
  128. }
  129. picker.dismiss(animated: true) {
  130. completion?()
  131. }
  132. }
  133. }
  134. extension MultiMediaManager: UINavigationControllerDelegate {
  135. }
  136. struct PermissionHelper {
  137. static func isAllowedToRecordVideo(completion: @escaping (Bool) -> ()) {
  138. let status = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
  139. completion(status != .denied && status != .restricted)
  140. }
  141. static func isAllowedToRecordSound(completion: @escaping (Bool) -> ()) {
  142. AVAudioSession.sharedInstance().requestRecordPermission(completion)
  143. }
  144. static func isAllowedToShowGallary(completion: @escaping (Bool) -> ()) {
  145. PHPhotoLibrary.requestAuthorization { (status) in
  146. completion(status == .authorized)
  147. }
  148. }
  149. }