繁体   English   中英

尝试从照片库将图像加载到UIImageView时,Swift 4出现错误

[英]Error with Swift 4 while trying to load image to UIImageView from photos library

我一直在尝试解决从照片应用程序库导入图片的错误。 我调查了一下,发现它有dictionary键值问题? 我很困惑,任何帮助都很棒。 下面的代码,以及错误。

@IBAction func onClickPickImage(_ sender: Any) {

    imagePicker.sourceType = .photoLibrary
    imagePicker.allowsEditing = true
    present(imagePicker, animated: true, completion: nil)

}
}

extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {

private func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    if let image = info[UIImagePickerControllerOriginalImage] as? Any {
        img.image = image
    }

    dismiss(animated: true, completion: nil)
}

错误 “无法用索引类型UIImagePickerController.InfoKey下标[String : Any]类型的值

应该是这样

let tempImage = info[UIImagePickerControllerOriginalImage] as! UIImage

对于可选绑定,请使用如下代码:

if let image = info[UIImagePickerControllerOriginalImage] as? UIImage{

}

最近,我还使用过照片库,因此希望对您有所帮助)

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        img.image = image
    }
    self.dismiss(animated: true, completion: nil)
}

@IBAction func onClickPickImage(_ sender: Any) {

    let image = UIImagePickerController()

    image.delegate = self

    image.sourceType = UIImagePickerControllerSourceType.photoLibrary

    image.allowsEditing = false

    self.present(image, animated: true){

    }

}

脚步:-

1. UINavigationControllerDelegate, UIImagePickerControllerDelegate 

2. var imagePicker              = UIImagePickerController() (Declare the variable)
3. let alert = UIAlertController(title: "Choose Image", message: nil, preferredStyle: .actionSheet)

            alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { _ in
                self.openCamera()
            }))

            alert.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { _ in
                self.openGallary()
            }))

            alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))
   self.present(alert, animated: true, completion: nil)

4. // MARK: -
    // MARK: - Image Picker Methods
    func openCamera() {
        if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)) {
            imagePicker.sourceType = UIImagePickerControllerSourceType.camera
            imagePicker.allowsEditing = true
            imagePicker.delegate = self
            self.present(imagePicker, animated: true, completion: nil)
        } else {
            let alert  = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
            self.present(alert, animated: true, completion: nil)
        }
    }

    func openGallary() {
        imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
        imagePicker.allowsEditing = true
        imagePicker.delegate = self
        self.present(imagePicker, animated: true, completion: nil)
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true, completion: nil)
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        NSLog("\(info)")
        let image = info[UIImagePickerControllerEditedImage] as? UIImage

     }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM