ios - 合成两个 CIImages 并在 UIImageView 问题中显示

标签 ios swift optimization cifilter ciimage

<分区>

我在使用 CIFilter 将两个图像组合在一起时遇到了问题。这里出了什么问题?

下面的代码创建了一个 UIImageView 并将其添加到 View 中,然后将两个图像 imageAimageBCIFilter 组合在一起,并将合成输出到 UIImageView

但是,合并后的图像没有显示在 UIImageView 中,它仍然是空白。


Questions:

  1. What is the correct code to display the composite image into the UIImageView?
  2. Is there more performant way in which to combine two images with CIFilter?

代码:

    let imageView = UIImageView()
    imageView.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height)
    imageView.contentMode = .Center
    view.addSubview(imageView)

    let imageA = CIImage(image: UIImage(named:"imageA")!)
    let imageB = CIImage(image: UIImage(named:"imageB")!)
    let imageFilter = CIFilter(name: "CIAdditionCompositing")!

    imageFilter.setValue(imageA, forKey: kCIInputImageKey)
    imageFilter.setValue(imageB, forKey: kCIInputBackgroundImageKey)

    if let imageCombined = imageFilter.outputImage {
        let image = UIImage(CIImage: imageCombined)
        imageView.image = image
    }

最佳答案

我一直对直接 UIImage << >> CIImage 转换有疑问。如果您有可以使用的框架/大小/范围,我建议您先使用 CIContext 并创建一个 CGImage:

let ciContext = CIContext(options: nil)
let cgImg = ciContext.createCGImage(imageCombined, from: imageCombined.extent)
let uiImage = UIImage(cgImage: cgImg!)

请记住,创建一个 CIContext 是“昂贵的”,因此如果您需要为多个图像执行此操作,请创建一个并将其用于所有渲染。此外,如果您在某处使用 GLKView,请使用它的上下文。

关于问题 #2,您的代码看起来不错。您可以“组合代码行,但这不会影响性能。在您调用过滤器的 outputImage 之前,CoreImage 不会“处理”任何内容。

关于ios - 合成两个 CIImages 并在 UIImageView 问题中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47871109/

相关文章:

swift - UICollectionviewcell 的子类无法实现为泛型

python - TensorFlow默认在线或批量训练

c# - 如何在 C# for iOS 中设置颜色在固定背景图像上的透明度级别?

iphone - 将 CG 结构序列化为适当对象的最佳实践

ios - Swift "Bridging-Header.h"文件不允许我在 .swift 文件中实例化 objective-c 类

ios - Swift - 布局 - 拉伸(stretch) UILabel

c# - object == object 而不是 object.id == object.id 潜在问题

删除空白的 asp.net 发布

objective-c - 静态库体积大

ios - 在 IOS 中的 tableview 顶部设置边距