ios5 - 从 CIImage 创建 UIImage

标签 ios5 uiimage core-image

我正在使用一些 CoreImage 过滤器来处理图像。将过滤器应用于我的输入图像会产生一个名为 filterOutputImage 的 CIImage 类型的输出图像。

我现在希望显示该图像,并尝试执行以下操作:

self.modifiedPhoto = [UIImage imageWithCIImage:filterOutputImage];
self.photoImageView.image = self.modifiedPhoto;

然而, View 是空白的 - 没有显示任何内容。

如果我添加打印出关于 filterOutputImage 和 self.modifiedPhoto 的详细信息的日志语句,这些日志语句会向我表明这两个变量似乎都包含合法的图像数据:它们的大小被报告并且对象不是 nil。

所以在做了一些谷歌搜索之后,我找到了一个需要经过 CGImage 阶段的解决方案;可见:
CGImageRef outputImageRef = [context createCGImage:filterOutputImage fromRect:[filterOutputImage extent]];
self.modifiedPhoto = [UIImage imageWithCGImage:outputImageRef scale:self.originalPhoto.scale orientation:self.originalPhoto.imageOrientation];
self.photoImageView.image = self.modifiedPhoto;
CGImageRelease(outputImageRef);

第二种方法有效:我在 View 中显示了正确的图像。

有人可以向我解释为什么我的第一次尝试失败了吗?我对 imageWithCIImage 方法做错了什么,导致图像似乎存在但无法显示?是否总是需要“通过”一个 CGImage 阶段才能从 CIImage 生成 UIImage?

希望有人能解决我的困惑:)

H。

最佳答案

我假设 self.photoImageView是 UIImageView 吗?如果是这样,最终它会调用 -[UIImage CGImage]在 UIImage 上,然后将该 CGImage 作为 contents 传递CALayer 的属性。

(见评论:我的详细信息有误)

根据 -[UIImage CGImage] 的 UIImage 文档:

If the UIImage object was initialized using a CIImage object, the
value of the property is NULL.

所以 UIImageView 调用了 -CGImage,但结果是 NULL,所以什么都不显示。

我还没有尝试过这个,但是你可以尝试制作一个自定义的 UIView,然后使用 UIImage 的 -draw... -[UIView drawRect:] 中的方法绘制 CIImage。

关于ios5 - 从 CIImage 创建 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9094058/

相关文章:

objective-c - cocoa 将 CIImage 保存为 bmp 文件图像损坏

ios - Objective-C : CIFilter returning nil image

iphone - 以编程方式在iPhone上创建声音

iphone - 如何使用 MapKit 在两个位置之间绘制路线并绘制要点?

ios - 如何从 HTML 字符串中获取 HREF 链接 url

ios - 如何获取使用 SDWebImage (iOS) 缓存的图像的文件系统路径

iphone - 将 CGPDFPage 渲染为 UIImage

iphone - 了解 UIImageView 中图像的真实边界

ios - CIQRCodeGenerator 提供不一致的结果

iphone - 在后台线程中运行方法后应用程序崩溃