189 lines
7.6 KiB
Swift
189 lines
7.6 KiB
Swift
//
|
|
// ImagePicker.swift
|
|
// MusicPlayer
|
|
//
|
|
// Created by Mr.Zhou on 2024/4/2.
|
|
//
|
|
|
|
import Foundation
|
|
import AVFoundation
|
|
import Photos
|
|
///扩展图片选择器
|
|
extension UIImagePickerControllerDelegate where Self:UIViewController{
|
|
///从相册选择
|
|
func getAlbum(){
|
|
let ImagePicker = UIImagePickerController()
|
|
//设置代理
|
|
ImagePicker.delegate = self as? UIImagePickerControllerDelegate & UINavigationControllerDelegate
|
|
//允许编辑
|
|
ImagePicker.allowsEditing = true
|
|
//设置图片源(照片库)
|
|
ImagePicker.sourceType = .photoLibrary
|
|
self.Albumpermissions {
|
|
//允许
|
|
//模态弹出IamgePickerView
|
|
DispatchQueue.main.async {
|
|
self.present(ImagePicker, animated: true, completion: nil)
|
|
}
|
|
} deniedBlock: {
|
|
self.album()
|
|
}
|
|
}
|
|
///照相机
|
|
func getCamera() {
|
|
guard UIImagePickerController.isSourceTypeAvailable(.camera) else {
|
|
//是模拟机
|
|
print("Is a virtual machine")
|
|
return
|
|
}
|
|
//是真机
|
|
let ImagePicker = UIImagePickerController()
|
|
//设置代理
|
|
ImagePicker.delegate = self as? UIImagePickerControllerDelegate & UINavigationControllerDelegate
|
|
//允许编辑
|
|
ImagePicker.allowsEditing = true
|
|
//设置图片源(照相)
|
|
ImagePicker.sourceType = .camera
|
|
self.Camerapermissions {
|
|
//模态弹出IamgePickerView
|
|
DispatchQueue.main.async {
|
|
self.present(ImagePicker, animated: true, completion: nil)
|
|
}
|
|
} deniedBlock: {
|
|
self.camera()
|
|
}
|
|
}
|
|
///选择视频
|
|
func getVideo(){
|
|
let ImagePicker = UIImagePickerController()
|
|
//设置代理
|
|
ImagePicker.delegate = self as? UIImagePickerControllerDelegate & UINavigationControllerDelegate
|
|
//禁止编辑
|
|
ImagePicker.allowsEditing = false
|
|
//设置来源
|
|
ImagePicker.sourceType = .photoLibrary
|
|
//设置显示视频文件
|
|
ImagePicker.mediaTypes = ["public.movie"]
|
|
|
|
self.Albumpermissions {
|
|
//允许
|
|
//模态弹出IamgePickerView
|
|
DispatchQueue.main.async {
|
|
self.present(ImagePicker, animated: true, completion: nil)
|
|
}
|
|
} deniedBlock: {
|
|
self.video()
|
|
}
|
|
}
|
|
// 相机权限
|
|
private func Camerapermissions(authorizedBlock: (() -> Void)?, deniedBlock: (() -> Void)?) {
|
|
let authStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
|
|
|
|
// .notDetermined .authorized .restricted .denied
|
|
if authStatus == .notDetermined {
|
|
// 第一次触发授权 alert
|
|
AVCaptureDevice.requestAccess(for: .video, completionHandler: { (granted: Bool) in
|
|
self.Camerapermissions(authorizedBlock: authorizedBlock, deniedBlock: deniedBlock)
|
|
})
|
|
} else if authStatus == .authorized {
|
|
if authorizedBlock != nil {
|
|
authorizedBlock!()
|
|
}
|
|
} else {
|
|
if deniedBlock != nil {
|
|
deniedBlock!()
|
|
}
|
|
}
|
|
}
|
|
// 相册权限
|
|
private func Albumpermissions(authorizedBlock: (() -> Void)?, deniedBlock: (() -> Void)?) {
|
|
let authStatus = PHPhotoLibrary.authorizationStatus()
|
|
|
|
// .notDetermined .authorized .restricted .denied
|
|
if authStatus == .notDetermined {
|
|
// 第一次触发授权 alert
|
|
PHPhotoLibrary.requestAuthorization { (status:PHAuthorizationStatus) -> Void in
|
|
self.Albumpermissions(authorizedBlock: authorizedBlock, deniedBlock: deniedBlock)
|
|
}
|
|
} else if authStatus == .authorized {
|
|
if authorizedBlock != nil {
|
|
authorizedBlock!()
|
|
}
|
|
} else {
|
|
if deniedBlock != nil {
|
|
deniedBlock!()
|
|
}
|
|
}
|
|
}
|
|
private func album(){
|
|
DispatchQueue.main.async {
|
|
//次要处理
|
|
let alertController = UIAlertController(title: "Access album permission request", message: "“Musiclax”currently does not have permission to access the album and cannot obtain album data. If you want to use the photo album function, please click “Settings” to allow this App to obtain permission to access the photo album", preferredStyle: .alert)
|
|
let CancelAction = UIAlertAction(title:"Cancel", style: .cancel) { (Cancel) in
|
|
|
|
}
|
|
let OKAction = UIAlertAction(title: "Settings", style: .default) { (action) in
|
|
let url = URL(string: UIApplication.openSettingsURLString)
|
|
if let url = url,UIApplication.shared.canOpenURL(url){
|
|
if #available(iOS 10, *) {
|
|
UIApplication.shared.open(url, options: [:]) { (success) in
|
|
}
|
|
}else{
|
|
UIApplication.shared.canOpenURL(url)
|
|
}
|
|
}
|
|
}
|
|
alertController.addAction(CancelAction)
|
|
alertController.addAction(OKAction)
|
|
self.present(alertController, animated: true, completion: nil)
|
|
}
|
|
}
|
|
private func camera(){
|
|
DispatchQueue.main.async {
|
|
//次要处理
|
|
let alertController = UIAlertController(title: "Access camera permission request", message: "“Musiclax”does not have access to the camera, and cannot call camera functions. If you want to use the camera function, please click “Settings” to allow this App to gain access to the camera", preferredStyle: .alert)
|
|
let CancelAction = UIAlertAction(title:"Cancel", style: .cancel) { (Cancel) in
|
|
|
|
}
|
|
let OKAction = UIAlertAction(title: "Settings", style: .default) { (action) in
|
|
let url = URL(string: UIApplication.openSettingsURLString)
|
|
if let url = url,UIApplication.shared.canOpenURL(url){
|
|
if #available(iOS 10, *) {
|
|
UIApplication.shared.open(url, options: [:]) { (success) in
|
|
}
|
|
}else{
|
|
UIApplication.shared.canOpenURL(url)
|
|
}
|
|
}
|
|
}
|
|
alertController.addAction(CancelAction)
|
|
alertController.addAction(OKAction)
|
|
self.present(alertController, animated: true, completion: nil)
|
|
}
|
|
}
|
|
private func video(){
|
|
DispatchQueue.main.async {
|
|
//次要处理
|
|
let alertController = UIAlertController(title: "Access album permission request", message: "“Musiclax” needs to open your album to get the photos that will be used to add your custom white noise. Please go to ”Settings“ to turn on this permission!", preferredStyle: .alert)
|
|
let CancelAction = UIAlertAction(title:"Cancel", style: .cancel) { (Cancel) in
|
|
|
|
}
|
|
let OKAction = UIAlertAction(title: "Settings", style: .default) { (action) in
|
|
let url = URL(string: UIApplication.openSettingsURLString)
|
|
if let url = url,UIApplication.shared.canOpenURL(url){
|
|
if #available(iOS 10, *) {
|
|
UIApplication.shared.open(url, options: [:]) { (success) in
|
|
}
|
|
}else{
|
|
UIApplication.shared.canOpenURL(url)
|
|
}
|
|
}
|
|
}
|
|
alertController.addAction(CancelAction)
|
|
alertController.addAction(OKAction)
|
|
self.present(alertController, animated: true, completion: nil)
|
|
}
|
|
}
|
|
}
|
|
|