[英]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. 我有一个带有这些内容的Swift 4.1(Xcode 9.3)Playground,它以一种非显而易见的方式失败了。
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
. 生成的错误
Failed to convert UIImage to PNG
。 For completeness, the returned place kitten is a JPG image. 为了完整,返回的地方小猫是JPG图像。
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: 虽然我无法告诉您原因,但为什么您的代码无法按预期工作,您可以使用以下解决方法检索位于特定URL的
CIImage
,如评论中所建议的:
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
为了简单起见,我把它留了下来
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.