【问题标题】:Alert not showing in the ImagePickerImagePicker 中未显示警报
【发布时间】:2020-06-16 03:26:29
【问题描述】:

我想提供使用照片库或相机的选项,但是当我添加当前操作表时,我没有显示选择照片或使用相机的提醒。编译后的应用程序直接进入相册,而我没有选择相机或照片库的选项。

这是来自我的 Xcode 11 的日志:

2020-06-15 23:05:22.931477-0400 MeMe[6298:3505455] 尝试呈现 正在等待延迟呈现 完成 2020-06-15 23:05:22.931653-0400 MeMe[6298:3505455] 尝试呈现 在 正在等待延迟呈现的完成 connection_block_invoke_2 中的错误: 连接中断

这是我的代码:

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate,
UINavigationControllerDelegate {

    // OUTLETS *******************************************

    // Image
    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    // ACTIONS *******************************************    

    // Button
    @IBAction func pickImage(_ sender: Any) {

        let imagePickerController = UIImagePickerController()
        // set imagePickerController as delegate
        imagePickerController.delegate = self

        // provide actionSheet to display the camera and photo  options
        let actionSheet = UIAlertController(title: "Source", message: "Take a picture or select a photo", preferredStyle: .actionSheet)

        // add camera action to imagePickerController
        actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler:{(action:UIAlertAction) in

            // check if the camera is available
            if UIImagePickerController.isSourceTypeAvailable(.camera) {
                imagePickerController.sourceType = .camera
            }
            else {
                print("Camera not available")
            }

        }))

        self.present(imagePickerController, animated: true, completion: nil)

        // add photos action to imagePickerController
        actionSheet.addAction(UIAlertAction(title: "Photos", style: .default, handler:{(action:UIAlertAction) in imagePickerController.sourceType = .photoLibrary}))

        self.present(imagePickerController, animated: true, completion: nil)

        // cancel
        actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

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

    // assign image to imageView

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

        let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage

        imageView.image = image

        picker.dismiss(animated: true, completion: nil)

    }

    // dismiss the image selector

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {

        picker.dismiss(animated: true, completion: nil)

    }


    }



【问题讨论】:

    标签: swift xcode camera imagepicker


    【解决方案1】:

    您在展示imagePickerController 后尝试展示actionSheet,这是不可能的。您需要在UIAlertAction 的处理程序块中显示imagePickerController。代码如下:

    @IBAction func pickImage(_ sender: Any) {
    
        let imagePickerController = UIImagePickerController()
        // set imagePickerController as delegate
        imagePickerController.delegate = self
    
        // provide actionSheet to display the camera and photo  options
        let actionSheet = UIAlertController(title: "Source", message: "Take a picture or select a photo", preferredStyle: .actionSheet)
    
        // add camera action to imagePickerController
        actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler:{(action:UIAlertAction) in
    
            // check if the camera is available
            if UIImagePickerController.isSourceTypeAvailable(.camera) {
                imagePickerController.sourceType = .camera
            }
            else {
                print("Camera not available")
            }
    
            self.present(imagePickerController, animated: true, completion: nil)
        }))
    
    
        // add photos action to imagePickerController
        actionSheet.addAction(UIAlertAction(title: "Photos", style: .default, handler:{(action:UIAlertAction) in
            imagePickerController.sourceType = .photoLibrary
            self.present(imagePickerController, animated: true, completion: nil)
        }))
    
    
        // cancel
        actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
    
        self.present(actionSheet, animated: true, completion: nil)
    }
    

    【讨论】:

      【解决方案2】:
      @IBAction func pickImage(_ sender: Any) {
      
      let imagePickerController = UIImagePickerController()
      imagePickerController.delegate = self
      
      
      let actionSheet = UIAlertController(title: "Source", message: "Take a picture or select a photo", preferredStyle: .actionSheet)
      
      
      actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler:{(action:UIAlertAction) in
      
      
          if UIImagePickerController.isSourceTypeAvailable(.camera) {
              imagePickerController.sourceType = .camera
            self.present(imagePickerController, animated: true, completion: nil)
          }  else {
              print("Camera not available")
          }
      
      }))
      
      
      
      
      actionSheet.addAction(UIAlertAction(title: "Photos", style: .default, handler:{(action:UIAlertAction) in   
      
      imagePickerController.sourceType = .photoLibrary
      self.present(imagePickerController, animated: true, completion: nil)
       }))
      
      // cancel
      actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
      
      self.present(actionSheet, animated: true, completion: nil)
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-18
        • 1970-01-01
        • 2011-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多