TSPhotoPickerManager.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // TSPhotoPickerManager.swift
  3. // AIEmoji
  4. //
  5. // Created by 100Years on 2025/2/25.
  6. //
  7. import UIKit
  8. import PhotosUI
  9. class TSPhotoPickerManager: NSObject {
  10. // MARK: - Properties
  11. private weak var viewController: UIViewController?
  12. private var completionHandler: ((UIImage?,PHAsset?) -> Void)?
  13. // MARK: - Initializers
  14. init(viewController: UIViewController) {
  15. self.viewController = viewController
  16. }
  17. // MARK: - Public Methods
  18. /// 打开照片选择器,单选一张照片
  19. func pickSinglePhoto(completion: @escaping (UIImage?,PHAsset?) -> Void) {
  20. self.completionHandler = completion
  21. // 检查相册权限
  22. checkPhotoLibraryPermission { [weak self] authorized in
  23. guard let self = self else { return }
  24. if authorized {
  25. self.openPhotoPicker()
  26. } else {
  27. self.showPermissionAlert()
  28. }
  29. }
  30. }
  31. // MARK: - Private Methods
  32. /// 检查相册权限
  33. private func checkPhotoLibraryPermission(completion: @escaping (Bool) -> Void) {
  34. let status = PHPhotoLibrary.authorizationStatus()
  35. switch status {
  36. case .authorized:
  37. completion(true)
  38. case .notDetermined:
  39. PHPhotoLibrary.requestAuthorization { newStatus in
  40. DispatchQueue.main.async {
  41. completion(newStatus == .authorized)
  42. }
  43. }
  44. default:
  45. completion(false)
  46. }
  47. }
  48. /// 打开照片选择器
  49. private func openPhotoPicker() {
  50. // if #available(iOS 14, *) {
  51. // var configuration = PHPickerConfiguration(photoLibrary: .shared())
  52. // configuration.selectionLimit = 1 // 只能选择一张照片
  53. // configuration.filter = .images // 只显示照片,不显示视频
  54. // configuration.preferredAssetRepresentationMode = .current // 使用高质量图片
  55. //
  56. // let picker = PHPickerViewController(configuration: configuration)
  57. // picker.delegate = self
  58. // viewController?.present(picker, animated: true, completion: nil)
  59. // } else {
  60. // iOS 14 以下使用旧的 UIImagePickerController
  61. let imagePicker = UIImagePickerController()
  62. imagePicker.sourceType = .photoLibrary
  63. imagePicker.delegate = self
  64. imagePicker.mediaTypes = ["public.image"] // 只选择照片
  65. viewController?.present(imagePicker, animated: true, completion: nil)
  66. // }
  67. }
  68. /// 显示权限提示
  69. private func showPermissionAlert() {
  70. let alert = UIAlertController(
  71. title: "相册权限未开启",
  72. message: "请在设置中开启相册权限以选择照片",
  73. preferredStyle: .alert
  74. )
  75. alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
  76. alert.addAction(UIAlertAction(title: "去设置", style: .default) { _ in
  77. if let url = URL(string: UIApplication.openSettingsURLString) {
  78. UIApplication.shared.open(url, options: [:], completionHandler: nil)
  79. }
  80. })
  81. viewController?.present(alert, animated: true, completion: nil)
  82. }
  83. }
  84. //// MARK: - PHPickerViewControllerDelegate (iOS 14+)
  85. //@available(iOS 14, *)
  86. //extension TSPhotoPickerManager: PHPickerViewControllerDelegate {
  87. // func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
  88. // picker.dismiss(animated: true) {
  89. // guard let result = results.first else {
  90. // self.completionHandler?(nil,nil)
  91. // return
  92. // }
  93. //
  94. // // 获取选中的图片
  95. // result.itemProvider.loadObject(ofClass: UIImage.self) { [weak self] (object, error) in
  96. // if let image = object as? UIImage {
  97. // DispatchQueue.main.async {
  98. // self?.completionHandler?(image)
  99. // }
  100. // } else {
  101. // DispatchQueue.main.async {
  102. // self?.completionHandler?(nil,nil)
  103. // }
  104. // }
  105. // }
  106. // }
  107. // }
  108. //}
  109. // MARK: - UIImagePickerControllerDelegate & UINavigationControllerDelegate (iOS 14 以下)
  110. extension TSPhotoPickerManager: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
  111. func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
  112. picker.dismiss(animated: true) {
  113. if let image = info[.originalImage] as? UIImage {
  114. self.completionHandler?(image,info[.phAsset] as? PHAsset )
  115. } else {
  116. self.completionHandler?(nil,nil)
  117. }
  118. }
  119. }
  120. func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
  121. picker.dismiss(animated: true, completion: nil)
  122. self.completionHandler?(nil,nil)
  123. }
  124. }