簡體   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