这是我的代码,可让用户选择个人资料照片:
早些时候,我只允许用户用相机拍照作为个人资料图片,但我决定更改它,以便用户现在可以从照片库中选择照片。
问题是我的用户能够访问照片库,而不会收到访问其照片库的请求警报。“某某应用程序想要访问您的照片库”。我有 Info.plist 用于照片库使用和照片库添加使用。此外,当我的应用程序在模拟器中运行时,当我进入“设置”然后进入“隐私”时,我会转到“照片”以查看我的应用程序是否具有访问权限,但那里没有任何内容提到我的应用程序可以访问照片库。
如果有人可以帮助我弄清楚我做错了什么以及如何让访问“照片库”的请求正常工作,我将不胜感激!谢谢。
class ProfileImageVC: UIViewController {
@IBOutlet weak var AddAProfilePictureLabel: UILabel!
@IBOutlet weak var ProfilePictureImageView: UIImageView!
@IBOutlet weak var TapToChangeButton: UIButton!
@IBOutlet var ErrorLabel: UILabel!
@IBOutlet var FinishButton: UIButton!
var ImagePicker:UIImagePickerController!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let imageTap = UITapGestureRecognizer(target: self, action: #selector(openImagePicker))
ProfilePictureImageView.isUserInteractionEnabled = true
ProfilePictureImageView.addGestureRecognizer(imageTap)
ProfilePictureImageView.layer.cornerRadius = ProfilePictureImageView.bounds.height / 2
ProfilePictureImageView.clipsToBounds = true
TapToChangeButton.addTarget(self, action: #selector(openImagePicker), for: .touchUpInside)
//when user clicks they can choose a photo from library
//instantiate image picker
ImagePicker = UIImagePickerController()
ImagePicker.allowsEditing = true
ImagePicker.delegate = self
}
// taping to add a photo
@objc func openImagePicker(_ sender:Any) {
// Open Image Picker
#if targetEnvironment(simulator)
// simulator
ImagePicker.sourceType = .photoLibrary
self.present(ImagePicker, animated: true, completion: nil)
#else
// real device
ImagePicker.sourceType = .photoLibrary
self.present(ImagePicker, animated: true, completion: nil)
#endif
}
func checkCameraAccess() {
switch AVCaptureDevice.authorizationStatus(for: .video) {
case .denied:
print("Denied, request permission from settings")
presentCameraSettings()
case .restricted:
print("Restricted, device owner must approve")
case .authorized:
self.present(ImagePicker, animated: true, completion: nil)
case .notDetermined:
AVCaptureDevice.requestAccess(for: .video) { success in
DispatchQueue.main.async {
if success {
self.present(self.ImagePicker, animated: true, completion: nil)
} else {
self.presentCameraSettings()
}
}
}
@unknown default:
break
}
}
func presentCameraSettings() {
let alertController = UIAlertController(title: "Error",
message: "Photo library access is denied",
preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Cancel", style: .default))
alertController.addAction(UIAlertAction(title: "Settings", style: .cancel) { _ in
if let url = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(url)
}
})
present(alertController, animated: true)
}
}
//extend the proper delagate method
extension ProfileImageVC: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
//cancel
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
//pick an image
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
//get the image the selected
if let pickedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
self.ProfilePictureImageView.image = pickedImage
picker.dismiss(animated: true, completion: nil)
startLoading(self.view)
//upload to firbase
PhotoService.savePhoto(image: pickedImage) { error in
stopLoading(self.view)
let resturantslocation =
self.storyboard?.instantiateViewController(identifier: Constants.Storyboard.userslocation) as? ResturantsUsersLocationVC
self.view.window?.rootViewController = resturantslocation
self.view.window?.makeKeyAndVisible()
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
\n\n问题是我的用户能够访问照片库,而不会收到访问其照片库的请求警报
\n
这不是一个“问题”。这是正确的行为。
\nUIImagePickerController(或者在 iOS 14 中为 PHPickerViewController)不需要照片库的用户授权。那是因为您的应用程序未访问照片库!运行时正在访问它,并且用户愿意给你一张照片。您收到的照片不是库 \xe2\x80\x94 中的完整照片(即 PHAsset \xe2\x80\x94),而是纯粹的 UIImage。所以你得到的只是无形的图像。
\n如果您想转身获取 PHAsset 并查看库,则需要用户授权。但你只是要求.editedImage,所以你不需要用户授权。
如果.editedImage是nil,那是因为钥匙错误;如果用户没有编辑图像,那么您想要的是.originalImage. 而且您仍然不需要授权。
如果您想要求系统显示请求授权的警报,请继续要求它这样做。但是用户不会仅仅因为您使用选择器就神奇地收到警报。
\n| 归档时间: |
|
| 查看次数: |
2356 次 |
| 最近记录: |