【问题标题】:When I present my UIImagePickerController, nothing comes up当我展示我的 UIImagePickerController 时,什么也没有出现
【发布时间】:2018-01-17 18:38:55
【问题描述】:

我想要做的是显示一个警报,用户可以在其中按下从相机或照片库获取照片。但是,当我按下库时, UIImagePickerViewController 不存在。我正在向 ALert 添加 UIAlert 操作,并且我传入的处理程序是作为呈现 ImagePicker 控制器的函数。我究竟做错了什么?我怀疑这与我呈现 ImagePickerViewController 的方式有关,但我不确定。我阅读了苹果文档,他们说弹出框是唯一呈现它的样式。

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
    }
}

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    永远不会显示图像选择器。在动作警报中,函数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
        }
    }
    

    【讨论】:

    • UIImagePickerController.isSourceTypeAvailable(.camera) -- 虽然这完全按照文档中的说明进行,但它什么也没做。
    • @7stud 谢谢。更新。那是对原始代码的盲目复制和粘贴。
    • @maddy,是的,我知道。
    • 很高兴为您提供帮助。作为新用户,我想提醒您,您可以通过单击最能回答您问题的答案左侧的复选标记来表明您的问题已得到解决。享受吧。
    【解决方案2】:

    示例 1:

    func doStuff(x: Int, handler: (Int) -> Void) {
            handler(x)
    }
    
    doStuff(x: 3) {val in
        print(val)
    }
    
    --output:--
    3
    

    示例 2:

    func doStuff(x: Int, handler: (Int) -> Void) {
            handler(x)
    }
    
    doStuff(x: 3) {val in
        func calc(a: Int, b: Int) {
            print(a + b)
        }
    }
    
    --output:--
    <Nothing>
    

    iOS 编程并不适合初学者——对于有经验的程序员来说很难。

    【讨论】:

      猜你喜欢
      • 2023-03-24
      • 2015-04-20
      • 1970-01-01
      • 1970-01-01
      • 2015-03-17
      • 2021-03-24
      • 2014-05-18
      • 2020-10-10
      • 1970-01-01
      相关资源
      最近更新 更多