|
@ -5,7 +5,6 @@ |
|
|
// Created by shishir sapkota |
|
|
// Created by shishir sapkota |
|
|
// |
|
|
// |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import UIKit |
|
|
import UIKit |
|
|
import Foundation |
|
|
import Foundation |
|
|
import AVFoundation |
|
|
import AVFoundation |
|
@ -40,14 +39,24 @@ class MultiMediaManager: NSObject { |
|
|
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad) { |
|
|
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad) { |
|
|
presentationStyle = .alert |
|
|
presentationStyle = .alert |
|
|
} |
|
|
} |
|
|
// |
|
|
|
|
|
let alertController = UIAlertController(title: nil, message: "Gme would like to access your camera", preferredStyle: presentationStyle) |
|
|
|
|
|
let cameraAction = UIAlertAction(title: "camera_text".localized(), style: UIAlertAction.Style.default) { (action) in |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let alertController = UIAlertController( |
|
|
|
|
|
title: nil, |
|
|
|
|
|
message: "Gme would like to access your camera", |
|
|
|
|
|
preferredStyle: presentationStyle |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
let cameraAction = UIAlertAction( |
|
|
|
|
|
title: "camera_text".localized(), |
|
|
|
|
|
style: UIAlertAction.Style.default |
|
|
|
|
|
) { (_) in |
|
|
self.openCamera(mode: .photo) |
|
|
self.openCamera(mode: .photo) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let galleryAction = UIAlertAction(title: "gallery_text".localized(), style: UIAlertAction.Style.default) { (action) in |
|
|
|
|
|
|
|
|
let galleryAction = UIAlertAction( |
|
|
|
|
|
title: "gallery_text".localized(), |
|
|
|
|
|
style: UIAlertAction.Style.default |
|
|
|
|
|
) { (_) in |
|
|
self.showPhotoGallery(mode: mode) |
|
|
self.showPhotoGallery(mode: mode) |
|
|
} |
|
|
} |
|
|
let cancelAction = UIAlertAction(title: "cancel_text".localized(), style: .cancel, handler: nil) |
|
|
let cancelAction = UIAlertAction(title: "cancel_text".localized(), style: .cancel, handler: nil) |
|
@ -57,7 +66,11 @@ class MultiMediaManager: NSObject { |
|
|
self.presentViewController(viewController: alertController, animated: true, completion: nil) |
|
|
self.presentViewController(viewController: alertController, animated: true, completion: nil) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private func presentViewController(viewController: UIViewController, animated: Bool, completion: (() -> Void)?) { |
|
|
|
|
|
|
|
|
private func presentViewController( |
|
|
|
|
|
viewController: UIViewController, |
|
|
|
|
|
animated: Bool, |
|
|
|
|
|
completion: (() -> Void)? |
|
|
|
|
|
) { |
|
|
OperationQueue.main.addOperation { |
|
|
OperationQueue.main.addOperation { |
|
|
self.presenter?.presenting().present(viewController, animated: animated, completion: completion) |
|
|
self.presenter?.presenting().present(viewController, animated: animated, completion: completion) |
|
|
} |
|
|
} |
|
@ -76,14 +89,9 @@ class MultiMediaManager: NSObject { |
|
|
PermissionHelper.isAllowedToRecordVideo { (isAllowed) in |
|
|
PermissionHelper.isAllowedToRecordVideo { (isAllowed) in |
|
|
if isAllowed { |
|
|
if isAllowed { |
|
|
openPicker() |
|
|
openPicker() |
|
|
}else { |
|
|
|
|
|
self.presenter?.presenting().alert(message: "camera_access_denied_text", okAction: { |
|
|
|
|
|
let url = URL(string: UIApplication.openSettingsURLString)! |
|
|
|
|
|
if #available(iOS 10.0, *) { |
|
|
|
|
|
UIApplication.shared.open(url, options: convertToUIApplicationOpenExternalURLOptionsKeyDictionary([:]), completionHandler: nil) |
|
|
|
|
|
} else { |
|
|
} else { |
|
|
// Fallback on earlier versions |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
self.presenter?.presenting().alert(message: "camera_access_denied_text", okAction: { |
|
|
|
|
|
UIApplication.tryURL(url: UIApplication.openSettingsURLString) |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -97,21 +105,18 @@ class MultiMediaManager: NSObject { |
|
|
let mediaTypes = mode == .video ? ["public.image", "public.movie"] : ["public.image"] |
|
|
let mediaTypes = mode == .video ? ["public.image", "public.movie"] : ["public.image"] |
|
|
self.imagePicker.mediaTypes = mediaTypes |
|
|
self.imagePicker.mediaTypes = mediaTypes |
|
|
self.presentViewController(viewController: self.imagePicker, animated: true, completion: nil) |
|
|
self.presentViewController(viewController: self.imagePicker, animated: true, completion: nil) |
|
|
}else { |
|
|
|
|
|
self.presenter?.presenting().alert(message: "galery_access_denied_text".localized(), okAction: { |
|
|
|
|
|
let url = URL(string: UIApplication.openSettingsURLString)! |
|
|
|
|
|
if #available(iOS 10.0, *) { |
|
|
|
|
|
UIApplication.shared.open(url, options: convertToUIApplicationOpenExternalURLOptionsKeyDictionary([:]), completionHandler: nil) |
|
|
|
|
|
} else { |
|
|
} else { |
|
|
// Fallback on earlier versions |
|
|
|
|
|
|
|
|
self.presenter?.presenting().alert( |
|
|
|
|
|
message: "galery_access_denied_text".localized(), |
|
|
|
|
|
okAction: { |
|
|
|
|
|
UIApplication.tryURL(url: UIApplication.openSettingsURLString) |
|
|
} |
|
|
} |
|
|
}) |
|
|
|
|
|
|
|
|
) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func resizeImage(image: UIImage, to targetSize: CGSize) -> UIImage? { |
|
|
func resizeImage(image: UIImage, to targetSize: CGSize) -> UIImage? { |
|
|
// This is the rect that we've calculated out and this is what is actually used below |
|
|
// This is the rect that we've calculated out and this is what is actually used below |
|
|
let rect = CGRect(origin: CGPoint.zero, size: targetSize) |
|
|
let rect = CGRect(origin: CGPoint.zero, size: targetSize) |
|
@ -125,34 +130,37 @@ class MultiMediaManager: NSObject { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
extension MultiMediaManager: UIImagePickerControllerDelegate { |
|
|
extension MultiMediaManager: UIImagePickerControllerDelegate { |
|
|
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { |
|
|
|
|
|
// Local variable inserted by Swift 4.2 migrator. |
|
|
|
|
|
let info = convertFromUIImagePickerControllerInfoKeyDictionary(info) |
|
|
|
|
|
|
|
|
func imagePickerController( |
|
|
|
|
|
_ picker: UIImagePickerController, |
|
|
|
|
|
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any] |
|
|
|
|
|
) { |
|
|
|
|
|
let info = Dictionary(uniqueKeysWithValues: info.map {key, value in (key.rawValue, value)}) |
|
|
|
|
|
|
|
|
var completion: (()->())? |
|
|
|
|
|
|
|
|
var completion: (() -> Void)? |
|
|
|
|
|
|
|
|
if let mediaType = info["UIImagePickerControllerMediaType"] as? String { |
|
|
if let mediaType = info["UIImagePickerControllerMediaType"] as? String { |
|
|
if mediaType == "public.image" { |
|
|
if mediaType == "public.image" { |
|
|
if let picture = info[convertFromUIImagePickerControllerInfoKey(UIImagePickerController.InfoKey.originalImage)] as? UIImage { |
|
|
|
|
|
|
|
|
if let picture = info[ |
|
|
|
|
|
UIImagePickerController.InfoKey.originalImage.rawValue |
|
|
|
|
|
] as? UIImage { |
|
|
if let cropsize = self.delegate?.cropSize?() { |
|
|
if let cropsize = self.delegate?.cropSize?() { |
|
|
if picture.size.width < cropsize.width || picture.size.height < cropsize.height { |
|
|
if picture.size.width < cropsize.width || picture.size.height < cropsize.height { |
|
|
self.delegate?.didFinishPickingWithError(error: "Image size must be greater than \(cropsize.width) X \(cropsize.height)") |
|
|
|
|
|
}else { |
|
|
|
|
|
|
|
|
let message = "Image size must be greater than \(cropsize.width) X \(cropsize.height)" |
|
|
|
|
|
self.delegate?.didFinishPickingWithError(error: message) |
|
|
|
|
|
} else { |
|
|
guard let resizedImage = self.resizeImage(image: picture, to: cropsize) else { return } |
|
|
guard let resizedImage = self.resizeImage(image: picture, to: cropsize) else { return } |
|
|
completion = { |
|
|
completion = { |
|
|
self.delegate?.didFinishPickingWithImage?(image: resizedImage) |
|
|
self.delegate?.didFinishPickingWithImage?(image: resizedImage) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// self.cropImage(image: picture) |
|
|
|
|
|
} |
|
|
} |
|
|
}else { |
|
|
|
|
|
|
|
|
} else { |
|
|
completion = { |
|
|
completion = { |
|
|
self.delegate?.didFinishPickingWithImage?(image: picture) |
|
|
self.delegate?.didFinishPickingWithImage?(image: picture) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}else if mediaType == "public.movie" { |
|
|
|
|
|
_ = info["UIImagePickerControllerMediaURL"] as! URL |
|
|
|
|
|
|
|
|
} else if mediaType == "public.movie" { |
|
|
|
|
|
// _ = info["UIImagePickerControllerMediaURL"] as! URL |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -167,35 +175,19 @@ extension MultiMediaManager: UINavigationControllerDelegate { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
struct PermissionHelper { |
|
|
struct PermissionHelper { |
|
|
static func isAllowedToRecordVideo(completion: @escaping (Bool) -> ()) { |
|
|
|
|
|
|
|
|
static func isAllowedToRecordVideo(completion: @escaping (Bool) -> Void) { |
|
|
let status = AVCaptureDevice.authorizationStatus(for: AVMediaType.video) |
|
|
let status = AVCaptureDevice.authorizationStatus(for: AVMediaType.video) |
|
|
completion(status != .denied && status != .restricted) |
|
|
completion(status != .denied && status != .restricted) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static func isAllowedToRecordSound(completion: @escaping (Bool) -> ()) { |
|
|
|
|
|
|
|
|
static func isAllowedToRecordSound(completion: @escaping (Bool) -> Void) { |
|
|
AVAudioSession.sharedInstance().requestRecordPermission(completion) |
|
|
AVAudioSession.sharedInstance().requestRecordPermission(completion) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static func isAllowedToShowGallary(completion: @escaping (Bool) -> ()) { |
|
|
|
|
|
|
|
|
static func isAllowedToShowGallary(completion: @escaping (Bool) -> Void) { |
|
|
PHPhotoLibrary.requestAuthorization { (status) in |
|
|
PHPhotoLibrary.requestAuthorization { (status) in |
|
|
completion(status == .authorized) |
|
|
completion(status == .authorized) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Helper function inserted by Swift 4.2 migrator. |
|
|
|
|
|
fileprivate func convertFromUIImagePickerControllerInfoKeyDictionary(_ input: [UIImagePickerController.InfoKey: Any]) -> [String: Any] { |
|
|
|
|
|
return Dictionary(uniqueKeysWithValues: input.map {key, value in (key.rawValue, value)}) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Helper function inserted by Swift 4.2 migrator. |
|
|
|
|
|
fileprivate func convertToUIApplicationOpenExternalURLOptionsKeyDictionary(_ input: [String: Any]) -> [UIApplication.OpenExternalURLOptionsKey: Any] { |
|
|
|
|
|
return Dictionary(uniqueKeysWithValues: input.map { key, value in (UIApplication.OpenExternalURLOptionsKey(rawValue: key), value)}) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Helper function inserted by Swift 4.2 migrator. |
|
|
|
|
|
fileprivate func convertFromUIImagePickerControllerInfoKey(_ input: UIImagePickerController.InfoKey) -> String { |
|
|
|
|
|
return input.rawValue |
|
|
|
|
|
} |
|
|
|