【问题标题】:Creating UIImage from CIImage从 CIImage 创建 UIImage
【发布时间】:2012-02-24 00:41:44
【问题描述】:

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

【问题讨论】:

    标签: ios5 uiimage core-image


    【解决方案1】:

    应该这样做!

    -(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;
    }
    

    【讨论】:

      【解决方案2】:

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

      (见cmets:我的详细信息有误)

      根据-[UIImage CGImage] 的 UIImage 文档:

      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。

      【讨论】:

      • 啊!谢谢你。是的,photoImageView 是一个 UIImageView。我不知道它使用了 UIImage 的 CGImage 属性。
      • 还有一个问题,在哪个文档中解释了 UIImageView 如何显示它的图像?我试图找出你从哪里学到的:)
      • 实际上,现在我实际查看了二进制代码:UIImageView 覆盖 -drawRect: 而不是在它自己的 CALayer 上调用 setContents:。看起来它最终调用了 UIImage 绘制方法,该方法抓取 CGImageRef 并绘制它。所以,同样的结果,但我的详细信息是错误的。
      • class-dump 也有帮助。 -[UIImageView drawRect:] 的存在对我来说是一个巨大的暗示,即我最初关于内容的陈述是错误的
      猜你喜欢
      • 2017-04-14
      • 2021-05-30
      • 2013-01-15
      • 2011-12-08
      • 1970-01-01
      • 2012-09-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      相关资源
      最近更新 更多