在swift 4中访问相机和照片库 [英] Accessing the camera and photo library in swift 4

查看:22
本文介绍了在swift 4中访问相机和照片库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码访问 swift4 中的相机和照片库

I'm trying to access both the camera and photo library in swift4 using the following code

let imagePickerController = UIImagePickerController()
    imagePickerController.delegate = self
    let alert = UIAlertController(title: "", message: "", preferredStyle: .actionSheet)
    alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(action: UIAlertAction) in
        imagePickerController.sourceType = .camera
        print(1)
    }))
    alert.addAction(UIAlertAction(title: "Photo Album", style: .default, handler: {(action: UIAlertAction) in
        imagePickerController.sourceType = .photoLibrary
    }))
    alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: nil))
    self.present(alert, animated: true, completion: nil)

但它不起作用.即使我确保 plist 具有相机和照片库授权,我也没有收到访问授权请求.我将代码稍微操作如下

but it's not working. I'm not get an access authorization request even though I made sure that the plist has the camera and photo library authorization. I manipulate the code a bit to the following

AVCaptureDevice.requestAccess(for: AVMediaType.video) { response in
    }
    let photos = PHPhotoLibrary.authorizationStatus()
    if photos == .notDetermined {
        PHPhotoLibrary.requestAuthorization({status in
        })
    }

    let imagePickerController = UIImagePickerController()
    imagePickerController.delegate = self
    let alert = UIAlertController(title: "", message: "", preferredStyle: .actionSheet)
    alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(action: UIAlertAction) in
        imagePickerController.sourceType = .camera
        print(1)
    }))
    alert.addAction(UIAlertAction(title: "Photo Album", style: .default, handler: {(action: UIAlertAction) in
        imagePickerController.sourceType = .photoLibrary
    }))
    alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: nil))
    self.present(alert, animated: true, completion: nil)

现在我收到了相机和照片库的授权请求,我可以看到 AlertView 但是当我按下相机或相册时,如图所示没有任何反应.

Now I am getting the authorization request for both cameras and photo library and I can see the AlertView but when I press camera or Photo Album as shown in the picture nothing happens.

我尝试了用于相机的设备和模拟器.

I tried on a device and simulator for the camera.

推荐答案

这里是从照片中加载图片的代码 &iOS 中的相机.

Here is the code to load image from photos & camera in iOS.

⁃ 你需要为你的 UIImageView 创建一个 outlet

⁃ You need to create an outlet of your UIImageView

⁃ 然后在图像视图上添加一个点击手势

⁃ Then add a tap gesture on to image view

⁃然后将点击手势与didTapOnImageView函数连接起来.

⁃ Then connect tap gesture with the didTapOnImageView function.

⁃然后将以下扩展添加到您的视图控制器中.

⁃ Then add the following extension to your view controller.

//MARK:- Image Picker
extension YourViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    //This is the tap gesture added on my UIImageView.
    @IBAction func didTapOnImageView(sender: UITapGestureRecognizer) {
        //call Alert function
        self.showAlert()
    }

    //Show alert to selected the media source type.
    private func showAlert() {

        let alert = UIAlertController(title: "Image Selection", message: "From where you want to pick this image?", preferredStyle: .actionSheet)
        alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(action: UIAlertAction) in
            self.getImage(fromSourceType: .camera)
        }))
        alert.addAction(UIAlertAction(title: "Photo Album", style: .default, handler: {(action: UIAlertAction) in
            self.getImage(fromSourceType: .photoLibrary)
        }))
        alert.addAction(UIAlertAction(title: "Cancel", style: .destructive, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }

    //get image from source type
    private func getImage(fromSourceType sourceType: UIImagePickerController.SourceType) {

        //Check is source type available
        if UIImagePickerController.isSourceTypeAvailable(sourceType) {

            let imagePickerController = UIImagePickerController()
            imagePickerController.delegate = self
            imagePickerController.sourceType = sourceType
            self.present(imagePickerController, animated: true, completion: nil)
        }
    }

    //MARK:- UIImagePickerViewDelegate.
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

        self.dismiss(animated: true) { [weak self] in

            guard let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else { return }
            //Setting image to your image view
            self?.profileImgView.image = image
        }
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }

}

注意:不要忘记在 info.plist 中添加隐私设置

Note: Don't forget to add the privacy settings in info.plist

隐私 - 相机使用说明

Privacy - Camera Usage Description

隐私 - 照片库使用说明

Privacy - Photo Library Usage Description

这篇关于在swift 4中访问相机和照片库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆