【发布时间】:2021-01-10 05:47:12
【问题描述】:
这是我的代码,可让用户选择个人资料照片:
之前我只允许用户用他们的相机拍照作为他们的个人资料图片,但我决定更改它,以便用户现在可以从照片库中选择一张照片。
问题是我的用户能够访问照片库,而不会收到访问其照片库的请求警报。 “应用某某想访问你的照片库”。我有用于照片库使用和照片库添加使用的 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()
}
}
}
}
【问题讨论】: