ProgramingTip

UIImagePickerController를 사용하여 "알 수없는 유형의 이미지 형식을 만드는 것은 오류입니다."

bestdevel 2020. 10. 17. 11:51
반응형

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
    }
}

솔루션 허용 으로 지트 앤드 라 Choudhary 스위프트 3 엑스 코드 8 works.Although, 나는이 경고를 생성하는 것으로 나타 났습니다 :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)

}

UIImagePickerControllerDelegate와 함께 UINavigationControllerDelegate를 추가하는 것을 잊었습니다.

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 형식으로 저장합니다.

콘솔은 여전히 ​​"[일반] 알 수없는 유형의 이미지 형식을 제거했지만 내 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



}

나를 위해 오류가 발생했습니다.

"치명적인 오류 :하지만 nil을 찾았습니다 ..."

이미지 선택기에서 이미지를 선택할 때.

문제를 해결하기 위해 imageView다시 콘센트를 만들었습니다 .

참고 URL : https://stackoverflow.com/questions/39009889/creating-an-image-format-with-an-unknown-type-is-an-error-with-uiimagepickerco

반응형