簡體   English   中英

使用UIImagePickerController“使用未知類型創建圖像格式是一個錯誤”

[英]“Creating an image format with an unknown type is an error” with UIImagePickerController

在iOS 10 Swift 3中從圖像選擇器中選擇圖像時出現錯誤 - Creating an image format with an unknown type is an error

 func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {

    imagePost.image = image
    self.dismiss(animated: true, completion: nil)
}

圖像未被選中和更新。 我需要幫助或建議才能知道在iOS10或Swift 3中是否更改了此方法的語法或任何內容,或者是否有其他方法可以執行此操作。

下面提到的代碼確實解決了我的問題 -

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        imagePost.image = image
    } else{
        print("Something went wrong")
    }

    self.dismiss(animated: true, completion: nil)
}

記得向自己添加委托

let picker = UIImagePickerController()
picker.delegate = self // delegate added

下面的代碼確實解決了問題:

如果用戶對所選圖像執行更改,則僅拉動該圖像,否則在沒有任何更改的情況下拉動原始圖像源,最后關閉圖像選取器視圖控制器。

public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]){
    if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
        imageView.image = image
    }
    else if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        imageView.image = image
    } else{
        print("Something went wrong")
    }

    self.dismiss(animated: true, completion: nil)
}

如果您允許編輯圖片imageController.allowsEditing = true那么您需要先獲取已編輯的圖像:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    picker.dismissViewControllerAnimated(true, completion: nil)

    if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
        imagePost.image = image
    } else if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        imagePost.image = image
    } else {
        imagePost.image = nil
    }
}

Jeetendra Choudhary 接受的解決方案很有效 。雖然在Xcode 8中使用Swift 3,我注意到它會生成一個警告: Instance method 'imagePickerController(_:didFinishPickingMediaWithInfo:)' nearly matches optional requirement 'imagePickerController(_:didFinishPickingMediaWithInfo:)' of protocol 'UIImagePickerControllerDelegate'

在此輸入圖像描述

並建議添加@nonobjc私有關鍵字以使警告靜音。如果您使用這些建議使警告靜音,則解決方案不再有效。

我發現照片庫中的默認圖像可以解決這個問題。 如果將圖像從計算機拖動到模擬器上並選擇它。 這個問題解決了

根據Abdurohman的回答,我改變了我的代碼並解決了問題,謝謝。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage

    photoImageView.image = selectedImage

    // Dismiss the picker.
    dismiss(animated: true, completion: nil)
}

將其添加到viewDidload()

imagepicker.delegate = self

在函數參數中添加“_”。

func imagePickerController(picker: UIImagePickerController ...

func imagePickerController(_ picker: UIImagePickerController ...

這對我有用:

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

如果這對任何人都有幫助,那么當我將UIImageView子類化以創建圓角視圖時,我就會發生這種情況。 當我在viewDidLoad()中添加以下行時也發生了這種情況

imageView.layer.cornerRadius = self.imageView.frame.size.width / 2

我最終在viewWillLayoutSubViews中添加了這一行並且它有效。 有關詳細信息,請參閱此

clipsToBounds導致UIImage無法在iOS10和XCode 8中顯示

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

         let image = info[UIImagePickerControllerOriginalImage] as? UIImage
        self.dismiss(animated: true, completion: nil)
        self.imageTook.image = image
    }

更改didFinishPickingMediaWithInfo info: [String : AnyObject] ,到didFinishPickingMediaWithInfo info: [String : Any]

對於帶有swift 3的Xcode 8.1,在更改委托方法后如下所示

改變你的

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {

    imagePost.image = image
    self.dismiss(animated: true, completion: nil)
}

功能

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

    {


imagePost.image = info[UIImagePickerControllerOriginalImage] as? UIImage
 picker.dismiss(animated: true, completion: nil)

}

我忘了添加UINavigationControllerDelegate和UIImagePickerControllerDelegate

class ViewController:UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate

你會在一開始就添加一個變量

var imagePicker = UIImagePickerController()

並設置委托並在viewdidload()中調用一個函數

 imagePicker.delegate = self
 viwImagePick()

然后解釋該功能為

  //ImagePicker
    func viwImagePick(){


        let alert = UIAlertController(title: nil, message: "Choose your source", preferredStyle: UIAlertControllerStyle.alert)

        alert.addAction(UIAlertAction(title: "Camera", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
            print("Camera selected")
            self.openCamera()

            //Code for Camera
            //cameraf
        })
        alert.addAction(UIAlertAction(title: "Photo library", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
            print("Photo selected")
            self.openGallary()

            //Code for Photo library
            //photolibaryss
        })


        self.present(alert, animated: true, completion: nil)
           }



    func openCamera()
    {
        imagePicker.sourceType = UIImagePickerControllerSourceType.camera
        if UIDevice.current.userInterfaceIdiom == .phone
        {
            self.present(imagePicker, animated: true, completion: nil)
        }
        else
        {
            let popover = UIPopoverController(contentViewController: imagePicker)

            popover.present(from: profileImgViw.frame, in: self.view, permittedArrowDirections: UIPopoverArrowDirection.any, animated: true)

        }
    }

    func openGallary()
    {
        imagePicker.sourceType = UIImagePickerControllerSourceType.savedPhotosAlbum
        if UIDevice.current.userInterfaceIdiom == .phone
        {
            self.present(imagePicker, animated: true, completion: nil)
        }
        else
        {
           let popover = UIPopoverController(contentViewController: imagePicker)

            popover.present(from: profileImgViw.frame, in: self.view, permittedArrowDirections: UIPopoverArrowDirection.any, animated: true)


        }
    }

現在,圖像將添加到庫中的圖像視圖中

我認為你錯過了photoImageView和圖像之間的聯系。

看看下面的截圖:

在此輸入圖像描述

在此輸入圖像描述

我認為你錯過了photoImageView和圖像之間的聯系。

看看下面的截圖:

在此輸入圖像描述

在此輸入圖像描述

祝好運!

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        imgViewPhoto.image = image
    } else{
        print("Something went wrong")
    }

    picker.dismiss(animated: true, completion: nil)

}

一定要包含UINavigationControllerDelegate委托。 這解決了我的問題。

試試下面

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    print("image selected");
    let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImag
    self.dismiss(animated: true, completion: nil)
   UIImg.image = selectedImage
}

這對我有用。 只需復制並粘貼即可。

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    // The info dictionary contains multiple representations of the image, and this uses the original.
        let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage

    // Set photoImageView to display the selected image.
     photoImageView.image = selectedImage

    // Dismiss the picker.
    dismiss(animated: true, completion: nil)
}

我做的是,一旦我從didFinishPickingMediaWithInfo獲得圖像,我打電話給:

func prepareImageForSaving(image:UIImage) {
    // create NSData from UIImage
    guard let imageData = UIImageJPEGRepresentation(image, 1) else {
        // handle failed conversion
        print("jpg error")
        return
    }

    self.saveImage(imageData: imageData as NSData)
}

func saveImage(imageData: NSData) {
    imageDatas = imageData
}

以NSData格式保存。

控制台仍警告我“[Generic]創建一個包含未知類型的圖像格式是一個錯誤”,但至少我的imageView會更新到所選圖像,而不是從viewDidLoad顯示前一個圖像。

//帶有集合視圖類的圖像選擇器ViewController:UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UICollectionViewDataSource,UICollectionViewDelegate {

@IBOutlet var img: UIImageView!

@IBOutlet weak var collview: UICollectionView!

var image = NSMutableArray()


let imgpkr = UIImagePickerController()

override func viewDidLoad() {
    super.viewDidLoad()

    imgpkr.delegate = self
}




@IBAction func btnselect(_ sender: UIButton) {


    imgpkr.allowsEditing = true // false
    imgpkr.sourceType = .photoLibrary
    imgpkr.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
    present(imgpkr, animated: true, completion: nil)

}


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

    let choose = info[UIImagePickerControllerOriginalImage]as!UIImage
    let edit = info[UIImagePickerControllerEditedImage]as!UIImage

    img.contentMode = .scaleAspectFit
    img.image = edit

    //MARK:- Add image in collview

    image.add(edit)
    collview.reloadData()

    dismiss(animated: true, completion: nil)

}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {

    dismiss(animated: true, completion: nil)
}

//MARK:- Collection View


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return image.count

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collview.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)as! CollectionViewCell1

    cell.img1.image = image.object(at: indexPath.item)as! UIImage

    return cell



}

對我來說,我收到了錯誤:

“致命的錯誤:意外地發現沒有...”

當你選擇imagein imagepicker時。

為了解決這個問題,我再次為imageView創建了插座。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM