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

时间:2018-07-28 03:08:17

标签: ios swift dictionary uiimagepickercontroller

我一直试图通过从照片应用程序库导入图片来修复错误。我调查了一下,发现它的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)
}

错误'无法为索引为[String : Any]的类型UIImagePickerController.InfoKey下标

3 个答案:

答案 0 :(得分:0)

应该是这样

forEach

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

let tempImage = info[UIImagePickerControllerOriginalImage] as! UIImage

答案 1 :(得分:0)

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

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){

    }

}

答案 2 :(得分:0)

步骤:-

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

     }