CIImage contentsOfUrl无法将UIImage转换为PNG

时间:2023-01-23 18:58:38

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. For completeness, the returned place kitten is a JPG image.

生成的错误无法将UIImage转换为PNG。为了完整,返回的地方小猫是JPG图像。

CIImage contentsOfUrl无法将UIImage转换为PNG

1 个解决方案

#1


0  

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

不要忘记添加足够的错误处理。为了简单起见,我把它留了下来

#1


0  

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

不要忘记添加足够的错误处理。为了简单起见,我把它留了下来