绘制CIImage的背景颜色

时间:2015-05-11 20:38:25

标签: ios core-image glkit

我使用了一些CIFilter来进行图像处理。我发现UIImageView太慢,无法实时显示输出图像。我已关注documentation并构建GLKView。现在表现非常完美,但旋转时图像以黑色矩形呈现。

enter image description here

如何将该颜色设置为背景(在本例中为绿色)或透明?

这是绘图代码段。我根本不熟悉图形域。

private class ImageView: GLKView {

    override init!(frame: CGRect, context: EAGLContext!) { // Called with `.OpenGLES3` context.
        ciContext = CIContext(EAGLContext: context, options: [kCIContextWorkingColorSpace: NSNull()])
        super.init(frame: frame, context: context)    
    }

    private let ciContext: CIContext

    private var image: CIImage? {
        didSet {
            setNeedsDisplay()
        }
    }

    private override func drawRect(rect: CGRect) {

        // Clear with `glClear`... Here is set color from `backgroundColor` (green) and cleared with it.

        if image != nil {                
            ciContext.drawImage(image!, inRect: { /* returned computed rect */ }(), fromRect: image!.extent())
        }
    }

    private override func layoutSubviews() {
        super.layoutSubviews()
        setNeedsDisplay()
    }
}

1 个答案:

答案 0 :(得分:2)

您是否设置了混合模式?

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBlendEquation(GL_FUNC_ADD);

选中此link以显示混合模式的视觉效果

如果您正在设置混合模式,您可以在drawRect方法中显示更多内容吗?