简体   繁体   中英

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.

失败的游乐场

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM