【问题标题】:No permission to pick a photo from the photo library无权从照片库中挑选照片
【发布时间】: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()
            }
        }
    }
}


【问题讨论】:

    标签: ios swift


    【解决方案1】:

    问题是我的用户能够访问照片库,而不会收到访问其照片库的请求警报

    这不是“问题”。这是正确的行为。

    UIImagePickerController(或者,在 iOS 14 中,PHPickerViewController)不需要照片库的用户授权。那是因为您的应用没有访问照片库!运行时正在访问它,用户愿意给你一张照片。你收到的照片,不是作为库中的完整照片——那将是一个 PHAsset——而是作为一个单纯的 UIImage。所以你得到的只是没有实体的图像。

    如果您想转身获取 PHAsset 并查看库,您需要用户授权。但是你只是要求.editedImage,所以你不需要用户授权。

    如果.editedImagenil,那是因为它是错误的键;如果用户没有编辑图像,你想要的是.originalImage。而且您仍然不需要授权。

    如果您想要求系统显示请求授权的警报,请继续要求它执行此操作。但是用户不会仅仅因为您使用了选择器而神奇地得到警报。

    【讨论】:

    • 啊,好的,非常感谢!!!!所以你的意思是我实际上不需要呈现这个警报?如果我想要求系统显示请求授权的警报,我必须在此代码中添加什么?
    • requestAuthorization怎么样?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多