// // 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) } }