jsdw_ios/QuickLocation/Component/ImagePicker/ImagePicker.swift

111 lines
3.8 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// ImagePicker.swift
// HealthyZG
//
// Created by on 2020/6/17.
// Copyright © 2020 Lin. All rights reserved.
//
import Foundation
import UIKit
class ImagePicker: NSObject {
///
var imageDidPicked: ((UIImage) -> Void)?
/// ActionSheet
func presentImagePickerPopup() {
let popup = ImagePickerPopup()
popup.cameraAction = { [weak self] in
guard let self = self else { return }
self.requestCameraAuthorization(delegate: self)
}
popup.albumAction = { [weak self] in
guard let self = self else { return}
self.requestAlbumAuthorization()
}
UIViewController.presentController(popup, animated: true)
}
///
func showCamera() {
requestCameraAuthorization(delegate: self)
}
///
func showAlbum() {
requestAlbumAuthorization()
}
///
func requestCameraAuthorization(delegate: (UIImagePickerControllerDelegate & UINavigationControllerDelegate)) {
Permission.cameraAuthorizationStatus { (status) in
guard status else {
Permission.openAppSetting(title: "", message: String(format: "PermissionCamera".localizedString, kAppName))
return
}
Permission.photosAuthorizationStatus { (status) in
guard status else {
Permission.openAppSetting(title: "", message: String(format: "PermissionPhotos".localizedString, kAppName))
return
}
self.presentCameraImagePicker()
}
}
}
///
func presentCameraImagePicker() {
let imagePickerController = UIImagePickerController()
imagePickerController.sourceType = .camera
imagePickerController.delegate = self
UIViewController.presentController(imagePickerController, animated: true)
// navigator.present(imagePickerController)
}
///
func requestAlbumAuthorization() {
Permission.photosAuthorizationStatus { (status) in
guard status else {
Permission.openAppSetting(title: "", message: String(format: "PermissionPhotos".localizedString, kAppName))
return
}
self.presentAlbumImagePicker()
}
}
///
func presentAlbumImagePicker() {
let imagePickerController = UIImagePickerController()
imagePickerController.sourceType = .photoLibrary
imagePickerController.delegate = self
// let nav = BaseNavigationController(rootViewController: imagePickerController)
// nav.modalPresentationStyle = .overFullScreen
UIViewController.presentController(imagePickerController, animated: true)
// navigator.present(imagePickerController)
}
}
extension ImagePicker: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
guard let type = info[.mediaType] as? String, type == "public.image",
let image = info[.originalImage] as? UIImage else {
picker.dismiss(animated: true, completion: nil)
return
}
DispatchQueue.global().async {
// if let newImage = image.resizeTo(CGSize(width: 1024.0, height: 1024.0), scale: false) {
self.imageDidPicked?(image)
// }
}
picker.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
}