-2

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

这是我的 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)

    }


    }



4

2 回答 2

0
@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)

}

于 2020-06-16T03:33:08.970 回答
0

您正在尝试呈现actionSheet后呈现imagePickerController,这是不可能的。您需要imagePickerControllerUIAlertAction的处理程序块中显示。这是代码:

@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)
}
于 2020-06-16T03:31:40.570 回答