Music_Player3/relax.offline.mp3.music/MP/Common/Extension(扩展)/ImagePicker.swift
2024-08-15 15:50:48 +08:00

189 lines
7.7 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".localizableString(), message: "\"Musiclax\" needs to open your album to get the resources that will be used to add your custom white noise. Please go to ”Settings“ to turn on this permission!".localizableString(), 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)
}
}
}