警告未显示在 ImagePicker 中

Alert not showing in the ImagePicker

我想提供使用照片库或相机的选项,但是当我添加当前的 actionSheet 时,我对 select 照片或使用相机的警告没有出现。编译后的应用程序直接进入相册,没有让我选择 select 相机或照片库。

这是我 Xcode 11:

的日志

2020-06-15 23:05:22.931477-0400 MeMe[6298:3505455] Attempt to present on which is waiting for a delayed presention of to complete 2020-06-15 23:05:22.931653-0400 MeMe[6298:3505455] Attempt to present on which is waiting for a delayed presention of to complete error in connection_block_invoke_2: Connection interrupted

这是我的代码:

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)

    }


    }



您试图在呈现 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)
}
@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)

}