2012-02-01 94 views
10

我正在使用一些CoreImage过滤器来处理图像。将过滤器应用于我的输入图像会产生名为filterIntputImage的输出图像,其类型为CIImage。从CIImage创建UIImage

我现在需要显示的图像,并试着做:但是

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

视图为空 - 正在显示任何内容。

如果我添加了打印出有关filterOutputImage和self.modifiedPhoto的详细信息的日志语句,那些日志语句显示这两个变量似乎都包含合法的图像数据:它们的大小正在报告中,对象不为零。

所以在做了一些谷歌搜索之后,我发现了一个需要经历CGImage阶段的解决方案; vis:

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方法的错误是什么导致图像似乎存在但无法显示?为了从CIImage生成UIImage,是否需要“通过”CGImage阶段?

希望有人能消除我的困惑:)

H.

回答

8

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

(见注释:我的个人资料是错误的)

%的UIImage的文档-[UIImage CGImage]

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

所以调用的UIImageView -CGImage,但导致NULL,所以不会显示任何内容。

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

+0

啊!谢谢。是的,photoImageView是一个UIImageView。我不知道它使用了UIImage的CGImage属性。 – Hamster 2012-02-01 10:42:54

+0

还有一个问题,在哪个文档中解释了UIImageView如何显示其图像?我试图找出你从哪里学到的东西:) – Hamster 2012-02-01 10:46:56

+0

实际上,现在我已经看到了二进制代码:UIImageView重写-drawRect:而不是在它自己的CALayer上调用setContents:。看起来它最终调用了UIImage绘图方法,它抓取CGImageRef并绘制它。所以,结果相同,但我的细节错了。 – iccir 2012-02-01 10:49:31

15

这应该做到!

-(UIImage*)makeUIImageFromCIImage:(CIImage*)ciImage 
{ 
    self.cicontext = [CIContext contextWithOptions:nil]; 
    // finally! 
    UIImage * returnImage; 

    CGImageRef processedCGImage = [self.cicontext createCGImage:ciImage 
                 fromRect:[ciImage extent]]; 

    returnImage = [UIImage imageWithCGImage:processedCGImage]; 
    CGImageRelease(processedCGImage); 

    return returnImage; 
}