我正在使用一些CoreImage过滤器来处理图像 . 将滤镜应用于输入图像会生成名为filterOutputImage的输出图像,类型为CIImage .
我现在希望显示该图像,并尝试:
self.modifiedPhoto = [UIImage imageWithCIImage:filterOutputImage];
self.photoImageView.image = self.modifiedPhoto;
但是视图是空白的 - 没有显示任何内容 .
如果我添加打印出有关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);
第二种方法有效:我在视图中显示正确的图像 .
有人可以向我解释为什么我的第一次尝试失败了吗?我使用imageWithCIImage方法做错了什么导致图像看起来存在但无法显示?是否总是需要“通过”CGImage阶段才能从CIImage生成UIImage?
希望有人能解决我的困惑:)
H .
2 回答
我假设
self.photoImageView
是一个UIImageView?如果是这样,最终,它将在UIImage上调用 - [UIImage CGImage],然后将该CGImage作为CALayer的contents属性传递 .(见评论:我的细节错了)
根据
-[UIImage CGImage]
的UIImage文档:因此UIImageView调用-CGImage,但结果为NULL,因此不会显示任何内容 .
我没试过这个,但你可以尝试制作一个自定义的UIView,然后在 - [UIView drawRect:]中使用UIImage的-draw ...方法来绘制CIImage .
这应该做到!