我要做的是显示一个警报,用户可以从相机或照片库中获取照片。但是,当我按library时,UIImagePickerViewController不存在。我正在向警报添加UIAlert操作,我传入的处理程序是一个函数,它表示ImagePicker控制器。我做错什么了?我怀疑这与我展示ImagePickerViewController的方式有关,但我不确定。我读了苹果的文档,他们说popover是展示它的唯一风格。

import UIKit
import Firebase

class SignupViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    @IBOutlet weak var ProfilePic: UIButton!

    //When button is pressed, alert will pop up

    @IBAction func bringUpAlert(_ sender: Any) {
        let alert = UIAlertController(title: "Profile Photo", message: nil, preferredStyle: .actionSheet)
        alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(action) -> Void in
            func showCamera(){
                let camera = UIImagePickerController()
                UIImagePickerController.isSourceTypeAvailable(.camera)
                camera.delegate = self
                camera.sourceType = .camera
                self.present(camera, animated: true, completion: nil)
            }
        }))

        alert.addAction(UIAlertAction(title: "Library", style: .default, handler: {(action) -> Void in
            func showLibrary(){
                if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
                    let library = UIImagePickerController()
                    library.delegate = self
                    library.sourceType = .photoLibrary
                    library.modalPresentationStyle = UIModalPresentationStyle.popover
                    self.present(library, animated: true, completion: nil)
                }
                func imagePickerController(_picker: UIImagePickerController, didFinishPickingMediaWithInfo: [String:Any]) {
                    let selectedImage = didFinishPickingMediaWithInfo[UIImagePickerControllerOriginalImage] as! UIImage
                    self.ProfilePic.setImage(selectedImage, for: .normal)
                }
            }
        }))

        self.present(alert, animated: true, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        //Customizing Profile Pic View
        ProfilePic.layer.cornerRadius = ProfilePic.frame.size.width / 2
        ProfilePic.clipsToBounds = true
        ProfilePic.layer.borderWidth = 3
        ProfilePic.layer.borderColor = UIColor.gray.cgColor
    }
}

最佳答案

你从来没有真正试图显示图像选择器。您在警报操作中不必要地定义了函数showCamera,但从未调用它。
正确的解决方法是删除函数。

alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(action) -> Void in
    let camera = UIImagePickerController()
    UIImagePickerController.isSourceTypeAvailable(.camera)
    camera.delegate = self
    camera.sourceType = .camera
    self.present(camera, animated: true, completion: nil)
}))

或者,将showCamera函数声明移到外部并从警报操作调用它。
func showCamera(){
    if UIImagePickerController.isSourceTypeAvailable(.camera) {
        let camera = UIImagePickerController()
        camera.delegate = self
        camera.sourceType = .camera
        self.present(camera, animated: true, completion: nil)
    }
}

alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(action) -> Void in
    showCamera()
}))

您需要对嵌入的showLibrary函数执行同样的操作。图像选择器委托方法也需要移动到类的顶层。
期末班:
class SignupViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    @IBOutlet weak var ProfilePic: UIButton!

    //When button is pressed, alert will pop up

    @IBAction func bringUpAlert(_ sender: Any) {
        let alert = UIAlertController(title: "Profile Photo", message: nil, preferredStyle: .actionSheet)
        alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(action) -> Void in
            self.showCamera()
        }))

        alert.addAction(UIAlertAction(title: "Library", style: .default, handler: {(action) -> Void in
            self.showLibrary()
        }))

        self.present(alert, animated: true, completion: nil)
    }

    func showCamera(){
        if UIImagePickerController.isSourceTypeAvailable(.camera) {
            let camera = UIImagePickerController()
            camera.delegate = self
            camera.sourceType = .camera
            self.present(camera, animated: true, completion: nil)
        }
    }

    func showLibrary(){
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
            let library = UIImagePickerController()
            library.delegate = self
            library.sourceType = .photoLibrary
            library.modalPresentationStyle = UIModalPresentationStyle.popover
            self.present(library, animated: true, completion: nil)

        }
    }

    func imagePickerController(_picker: UIImagePickerController, didFinishPickingMediaWithInfo: [String:Any]) {
        let selectedImage = didFinishPickingMediaWithInfo[UIImagePickerControllerOriginalImage] as! UIImage
        self.ProfilePic.setImage(selectedImage, for: .normal)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        //Customizing Profile Pic View
        ProfilePic.layer.cornerRadius = ProfilePic.frame.size.width / 2
        ProfilePic.clipsToBounds = true
        ProfilePic.layer.borderWidth = 3
        ProfilePic.layer.borderColor = UIColor.gray.cgColor
    }
}

09-08 08:42