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