绘制CIImage的背景色

Background color for drawing CIImage

我使用一些 CIFilter 来做一些图像处理。我发现 UIImageView 太慢无法实时显示输出图像。我已关注 documentation 并构建了 GLKView。现在性能很完美,但是图像在旋转时呈现为黑色矩形。

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

这里是绘图代码片段。我对图形领域一点都不熟悉

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

您要设置混合模式吗?

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

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

如果您正在设置混合模式,您能否展示更多 drawRect 方法中发生的事情?