Brett
Brett

Reputation: 2399

CIImage contentsOfUrl Failed to convert UIImage to PNG

I have a Swift 4.1 (Xcode 9.3) Playground with these contents, and it's failing in a non-obvious way.

import UIKit

let url: URL = URL(string: "https://placekitten.com/500/500")!
let image: CIImage = CIImage(contentsOf: url)!

The resulting error is Failed to convert UIImage to PNG. For completeness, the returned place kitten is a JPG image.

Failed Playground

Upvotes: 9

Views: 1198

Answers (1)

jraufeisen
jraufeisen

Reputation: 3877

Whereas I cannot tell you the reason, for why your code does not work as expected, you can use the following workaround to retrieve a CIImage located at a specific URL as suggested in the comments:

import UIKit

let url: URL = URL(string: "https://placekitten.com/500/500")!
let image = UIImage.init(data: try! Data.init(contentsOf: url))!
let convertedImage = CIImage.init(image: image)

Don't forget to add adequate error handling. I left that out for the sake of simplicity

Upvotes: 1

Related Questions