CIImage的PNG/JPEG表示总是返回nil

编程入门 行业动态 更新时间:2024-10-25 13:25:01
本文介绍了CIImage的PNG/JPEG表示总是返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前正在制作照片编辑应用.

I'm currently making a photo editing app.

用户选择了照片后,将使用以下代码将其自动转换为黑白:

When a photo is selected by the user, it is automatically converted into black and white using this code:

func blackWhiteImage(image: UIImage) -> Data { print("Starting black & white") let orgImg = CIImage(image: image) let bnwImg = orgImg?.applyingFilter("CIColorControls", withInputParameters: [kCIInputSaturationKey:0.0]) let outputImage = UIImage(ciImage: bnwImg!) print("Black & white complete") return UIImagePNGRepresentation(outputImage)! }

此代码存在的问题是我不断收到此错误:

The problem I am having with this code is that I keep getting this error:

fatal error: unexpectedly found nil while unwrapping an Optional value

我的代码配置稍有不同,但是到达UIImagePNG/JPEGRepresentation(xx)部分时,它仍然会中断.

I have had my code in a slightly different configuration, but it still breaks when it gets to the UIImagePNG/JPEGRepresentation(xx) section.

是否有任何方法可以从CIImage获取PNG或JPEG数据以用于图像视图(通常仅是UIImage)?

Are there any ways to get the PNG or JPEG data from a CIImage for use in an image view / just UIImage in general?

其他任何方法都没有为应该使用什么代码提供足够的细节.

Any of the other methods don't go into enough detail for what code should be used.

推荐答案

只需开始一个新的图形上下文并在其中绘制灰度图像. iOS 10或更高版本,您可以使用UIGraphicsImageRenderer,对于较旧的iOS版本语法,请检查编辑历史记录:

Just begin a new graphics context and draw your grayscale image there. iOS 10 or later you can use UIGraphicsImageRenderer, for older iOS version syntax please check edit history:

Xcode 11•Swift 5.1

func blackWhiteImage(image: UIImage, isOpaque: Bool = false) -> Data? { guard let ciImage = CIImage(image: image)?.applyingFilter("CIColorControls", parameters: [kCIInputSaturationKey: 0]) else { return nil } let format = image.imageRendererFormat format.opaque = isOpaque return UIGraphicsImageRenderer(size: image.size, format: format).image { _ in UIImage(ciImage: ciImage).draw(in: CGRect(origin: .zero, size: image.size)) }.pngData() }

您还可以扩展UIImage以返回灰度图像:

You can also extend UIImage to return a grayscale image :

extension UIImage { var ciImage: CIImage? { CIImage(image: self) } func grayscale(isOpaque: Bool = false) -> UIImage? { guard let ciImage = ciImage?.applyingFilter("CIColorControls", parameters: [kCIInputSaturationKey: 0]) else { return nil } let format = imageRendererFormat format.opaque = isOpaque return UIGraphicsImageRenderer(size: size, format: format).image { _ in UIImage(ciImage: ciImage).draw(in: CGRect(origin: .zero, size: size)) } } }

let profilePicture = UIImage(data: try! Data(contentsOf: URL(string:"i.stack.imgur/Xs4RX.jpg")!))! if let grayscale = profilePicture.grayscale(), let data = grayscale.pngData() { // or Swift 4.1 or earlier -> let data = UIImagePNGRepresentation(grayscale) print(data.count) // 689035 }

更多推荐

CIImage的PNG/JPEG表示总是返回nil

本文发布于:2023-10-19 19:20:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508514.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PNG   CIImage   nil   JPEG

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!