从CIImage创建UIImage [英] Creating UIImage from CIImage
问题描述
我正在使用一些CoreImage过滤器来处理图像。将滤镜应用于输入图像会产生一个名为filterOutputImage的输出图像,类型为CIImage。
I am using some CoreImage filters to process an image. Applying the filter to my input image results in an output image called filterOutputImage of type CIImage.
我现在希望显示该图像,并尝试执行以下操作:
I now wish to display that image, and tried doing:
self.modifiedPhoto = [UIImage imageWithCIImage:filterOutputImage];
self.photoImageView.image = self.modifiedPhoto;
视图为空白 - 没有显示任何内容。
The view however is blank - nothing is being displayed.
如果我添加了打印出有关filterOutputImage和self.modifiedPhoto的详细信息的日志语句,那些日志语句会向我显示这些变量似乎都包含合法的图像数据:它们的大小正在报告且对象不是零。
If I add logging statements that print out details about both filterOutputImage and self.modifiedPhoto, those logging statements are showing me that both those vars appear to contain legitimate image data: their size is being reported and the objects are not nil.
因此,在做了一些谷歌搜索后,我找到了一个需要通过CGImage阶段的解决方案; vis:
So after doing some Googling, I found a solution that requires going through a CGImage stage; 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);
第二种方法有效:我在视图中显示正确的图像。
This second approach works: I am getting the correct image displayed in the view.
有人可以向我解释为什么我的第一次尝试失败了吗?我使用imageWithCIImage方法做错了什么导致图像看起来存在但无法显示?是否总是需要通过CGImage阶段才能从CIImage生成UIImage?
Can someone please explain to me why my first attempt failed? What am I doing wrong with the imageWithCIImage method that is resulting in an image that seems to exist but can't be displayed? Is it always necessary to "pass through" a CGImage stage in order to generate a UIImage from a CIImage?
希望有人能解决我的困惑:)
Hoping someone can clear up my confusion :)
H。
推荐答案
我假设 self.photoImageView
是UIImageView吗? 如果是这样,最终,它将在UIImage上调用 - [UIImage CGImage]
,然后将该CGImage作为内容传递
CALayer的财产。
I assume that self.photoImageView
is a UIImageView? If so, ultimately, it is going to call -[UIImage CGImage]
on the UIImage and then pass that CGImage as the contents
property of a CALayer.
(见评论:我的详细信息错误)
(See comments: my details were wrong)
根据的UIImage文档 - [UIImage CGImage]
:
If the UIImage object was initialized using a CIImage object, the
value of the property is NULL.
因此UIImageView调用-CGImage,但结果为NULL,因此不会显示任何内容。
So the UIImageView calls -CGImage, but that results in NULL, so nothing is displayed.
我没试过这个,但是你可以尝试制作一个自定义的UIView然后使用UIImage的 -draw ...
中的方法 - [UIView drawRect:]
绘制CIImage。
I haven't tried this, but you could try making a custom UIView and then using UIImage's -draw...
methods in -[UIView drawRect:]
to draw the CIImage.
这篇关于从CIImage创建UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!