我正在使用一些 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/