从CIImage创建UIImage

Ham*_*ter 10 core-image uiimage ios5

我正在使用一些CoreImage过滤器来处理图像.将滤镜应用于输入图像会生成名为filterOutputImage的输出图像,类型为CIImage.

我现在希望显示该图像,并尝试:

self.modifiedPhoto = [UIImage imageWithCIImage:filterOutputImage];
self.photoImageView.image = self.modifiedPhoto;
Run Code Online (Sandbox Code Playgroud)

但是视图是空白的 - 没有显示任何内容.

如果我添加了打印出有关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);
Run Code Online (Sandbox Code Playgroud)

第二种方法有效:我在视图中显示正确的图像.

有人可以向我解释为什么我的第一次尝试失败了吗?我使用imageWithCIImage方法做错了什么导致图像看起来存在但无法显示?是否总是需要"通过"CGImage阶段才能从CIImage生成UIImage?

希望有人能解决我的困惑:)

H.

Ton*_*ion 16

这应该做到!

-(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;
}
Run Code Online (Sandbox Code Playgroud)


icc*_*cir 8

我假设这self.photoImageView是一个UIImageView? 如果是这样,最终,它将调用-[UIImage CGImage]UIImage,然后将该CGImage作为contentsCALayer 的属性传递.

(见评论:我的细节错了)

根据UIImage文档-[UIImage CGImage]:

If the UIImage object was initialized using a CIImage object, the
value of the property is NULL.
Run Code Online (Sandbox Code Playgroud)

因此UIImageView调用-CGImage,但结果为NULL,因此不显示任何内容.

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