Creating an image format with an unknown type is a

2020-05-08 07:03发布

Swift Version 3
Xcode Version 8.3.3 (8E3004b)

This question has been asked and answered before. Yet, I cannot find a way fix this error. "Creating an image format with an unknown type is an error" with UIImagePickerController

When a new picture is selected from Photo Library, I get this error

Generic Creating an image format with an unknown type is an error.

I select the photo and when I tap choose, I get error in func takePhoto where I do this assignment self?.imgView.image = newImage

fatal error: unexpectedly found nil while unwrapping an Optional value Printing description of newImage: expression produced error: error: /var/folders/s0/xq_zc7l56m1__n1qjk29p6tw0000gn/T/./lldb/2631/expr1.swift:1:80: error: use of undeclared type 'UIKit' Swift._DebuggerSupport.stringForPrintObject(Swift.UnsafePointer>(bitPattern: 0x118055200)!.pointee) ^~~~~

typealias imagePickerHelperCompletion = ((UIImage?) -> Void)!

class ImagePickerHelper: NSObject {

 weak var viewController: UIViewController!
 var cameraFlashMode: UIImagePickerControllerCameraFlashMode   //flash, no flash
 var photoLibraryIsVisible: Bool
 var cameraDevice: UIImagePickerControllerCameraDevice //front, back camera
 var completion: imagePickerHelperCompletion


   init(viewController: UIViewController, cameraFlashMode:UIImagePickerControllerCameraFlashMode, photoLibraryIsVisible: Bool,cameraDevice: UIImagePickerControllerCameraDevice, completion: imagePickerHelperCompletion) {

    self.viewController = viewController
    self.completion = completion
    self.cameraFlashMode = cameraFlashMode
    self.photoLibraryIsVisible = photoLibraryIsVisible
    self.cameraDevice = cameraDevice

    //super.init() implemented by subclasses to initializer a new object (the receiver) immediately after memory has been allocated
    super.init()

    self.showPhotoSourceSelection()
}


//show camera or photo library
func showPhotoSourceSelection() {

    let actionSheet = UIAlertController(title: "Take new photo", message: "Would you like to open \n Camera or Photo Library?", preferredStyle: .actionSheet)

    let cameraAction = UIAlertAction(title: "Camera", style: .default) { (action) in
        self.showImagePicker(sourceType: .camera)
    }

    let photoLibraryAction = UIAlertAction(title: "Photo Library", style: .default) { (action) in
        self.showImagePicker(sourceType: .photoLibrary)
    }

    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in  }

        actionSheet.addAction(cameraAction)
    if photoLibraryIsVisible == true { actionSheet.addAction(photoLibraryAction) }
        actionSheet.addAction(cancelAction)
      viewController.present(actionSheet, animated: true, completion: nil)
}



func showImagePicker(sourceType: UIImagePickerControllerSourceType) {

    let imagePicker = UIImagePickerController()
    imagePicker.allowsEditing = true //user can crop the image, or do some editing
    imagePicker.sourceType = sourceType
    imagePicker.mediaTypes = [kUTTypeImage as String]
    imagePicker.delegate = self

      if imagePicker.sourceType == .camera {
         imagePicker.cameraFlashMode = cameraFlashMode
         imagePicker.cameraDevice = self.cameraDevice
    }
    viewController.present(imagePicker, animated: true, completion: nil)
 }
}//end of class



 extension ImagePickerHelper: UIImagePickerControllerDelegate, UINavigationControllerDelegate {

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



//if user did take a photo
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    print(info)
  //prints out ["UIImagePickerControllerEditedImage": <UIImage: 0x610000294320> size {746, 498} orientation 0 scale 1.000000, "UIImagePickerControllerMediaType": public.image, "UIImagePickerControllerCropRect": NSRect: {{0, 0}, {2661, 1779}}, "UIImagePickerControllerReferenceURL": assets-library://asset/asset.JPG?id=ED7AC36B-A150-4C38-BB8C-B6D696F4F2ED&ext=JPG, "UIImagePickerControllerOriginalImage": <UIImage: 0x610000299320> size {2668, 1780} orientation 0 scale 1.000000]



    if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
        completion(image)
        viewController.dismiss(animated: true, completion: nil)

    } else if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
          viewController.dismiss(animated: true, completion: nil)
          completion(image)

        } else {
        print("something is wrong line \(#line)")
       }
    }


}//end of extension






class ProfileViewController: UITableViewController {

  var imagePickerHelper: ImagePickerHelper!
  override func viewDidLoad() {
     super.viewDidLoad()
 }

  @IBAction func takePhoto(_ sender: Any) {

     //create instance of ImagePickerHelper and take photo, or get it from photoLibrary
      imagePickerHelper = ImagePickerHelper(  viewController: self, cameraFlashMode: .auto,photoLibraryIsVisible: true, cameraDevice: .front,
        completion: { [weak self] (newImage) in
     })
   }
}//end of class

0条回答
登录 后发表回答