swift 'UIImage?' is not convertible to 'UIImage'

swift apple
download swift
swift company
swift payment
swift wiki
swift tutorial
swift syntax
swift money transfer

I am getting the error 'UIImage?' is not convertible to 'UIImage' for the line let selectedImage = info[.originalImage] as? UIImage in my image picker function (below)

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

    let selectedImage = info[.originalImage] as? UIImage

    // how do i set the image on my buttons when I select a photo?
    let imageButton = (picker as? CustomImagePickerController)?.imageButton
    imageButton?.setImage(selectedImage?.withRenderingMode(.alwaysOriginal), for: .normal)
    dismiss(animated: true)

    let filename = UUID().uuidString
    let ref = Storage.storage().reference(withPath: "/images/\(filename)")
    guard let uploadData = selectedImage?.jpegData(compressionQuality: 0.75) else { return }


    //guard let uploadData = selectedImage else { return }

    let hud = JGProgressHUD(style: .dark)
    hud.textLabel.text = "Uploading image..."
    hud.show(in: view)
    ref.putData(uploadData, metadata: nil) { (nil, err) in
        if let err = err {
            hud.dismiss()
            print("Failed to upload image to storage:", err)
            return
        }

        print("Finished uploading image")
        ref.downloadURL(completion: { (url, err) in

            hud.dismiss()

            if let err = err {
                print("Failed to retrieve download URL:", err)
                return
            }

            print("Finished getting download url:", url?.absoluteString ?? "")

            if imageButton == self.image1Button {
                self.user?.imageUrl1 = url?.absoluteString
            } else if imageButton == self.image2Button {
                self.user?.imageUrl2 = url?.absoluteString
            } else {
                self.user?.imageUrl3 = url?.absoluteString
            }
        })
    }
}

Ran into the same error - next to the unwrapping of the selectedImage, make sure you have the correct/updated function parameters (they changed the info from type [String : Any] to [UIImagePickerController.InfoKey : Any] which was giving me the error.

This is how it should look like:

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

    guard let selectedImage = info[.originalImage] as? UIImage else {
        print("Error: \(info)")
        return
    }

    // do your thing...

    dismiss(animated: true, completion: nil)
}

Hope this helps!

SWIFT/BIC codes - What are SWIFT/BIC codes?, is similar to, and highly compatible with, the Objective-C programming language. Definition of swift. (Entry 1 of 5) 1 : moving or capable of moving with great speed a swift runner. 2 : occurring suddenly or within a very short time a swift transition. 3 : quick to respond : ready.

The signature of:

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

has been changed to:

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

which is the cause of error.

Changing the signature to the new one, i.e:

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

will resolve the error.

What is Swift?, does not have any kind of dependencies like Objective-C, which makes it a lot easier to maintain. Swift makes it easy to write software that is incredibly fast and safe by design. Our goals for Swift are ambitious: we want to make programming simple things easy, and difficult things possible. For students, learning Swift has been a great introduction to modern programming concepts and best practices.

This should work:

guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else { return }

Why is Swift so difficult to learn when Apple claims it is easy?, Mastering Swift 5: Deep Dive Into the Latest Edition of the Swift Programming Language, 5th Edition No swift species has become extinct since 1600, but BirdLife International assesses the Guam swiftlet as endangered and lists the Atiu, dark-rumped, Schouteden's, Seychelles and Tahiti swiftlets as vulnerable; twelve other species are near threatened or lack sufficient data for classification.

info is changed in info: [String : Any] (thanks apple to change every time)

override your func in the new one:

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

Is Swift a good language for a beginner to learn?, Swift: The Complete Guide for Beginners, Intermediate and Advanced Detailed Strategies To Master Swift Programming Swift is a fast and efficient language that provides real-time feedback and can be seamlessly incorporated into existing Objective-C code. So developers are able to write safer, more reliable code, save time, and create even richer app experiences. A beautiful app using Swift.

That's because you need to unwrap selectedImage first:

guard let selectedImage = info[.originalImage] as? UIImage else { return }

SWIFT – The global provider of secure financial messaging services, IOS 10 Programming Fundamentals with Swift: Swift, Xcode, and Cocoa Basics Swift is a powerful and intuitive programming language for iOS, macOS, tvOS, and watchOS. Writing Swift code is interactive and fun, the syntax is concise yet expressive, and Swift includes modern features developers love. Swift code is safe by design, yet also produces software that runs lightning-fast.

Swift.org, Swift is a fantastic way to write software, whether it's for phones, desktops, servers, or anything else that runs code. It's a safe, fast, and interactive programming 

About Swift, Society for Worldwide Interbank Financial Telecommunications (SWIFT) has become the global standard for processing instructions and 

Society for Worldwide Interbank Financial Telecommunication , Pull requests 470 Security 0 Pulse. The Swift Programming Language - https://​swift.org Latest commit by swift-ci about 1 hour ago. View code Jump to file 

Comments
  • Did you remove the optional question marks on selectedImage after unwrapping?
  • yes i still get 'UIImage?' is not convertible to 'UIImage'
  • im also getting a warning Cast from 'Slice<Dictionary<UIImagePickerController.InfoKey, Any>>' (aka 'Slice<Dictionary<NSString, Any>>') to unrelated type 'UIImage' always fails
  • So your problem is that you are trying to convert something that is not an UIImage to the UIImage type.
  • thanks Vasil That appears to be what xcode is telling me though how do I fix it?