Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 762
Brett Avatar asked Apr 23 '18 04:04

Brett


1 Answers

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

like image 130
JoRa Avatar answered Oct 18 '22 01:10

JoRa