Search code examples
ioscore-imageglkit

Background color for drawing CIImage


I use some of CIFilters to do some image processing. I've found UIImageView too slow to display output image in real-time. I've followed documentation and built GLKView. Now the performance is perfect, but image is rendered in black rect when rotated.

enter image description here

How to set that color to background (green in this case) or transparent?

Here is drawing code snippet. I'm not familiar with graphics domain at all.

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

Solution

  • Are you setting a blend mode?

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

    Check this link to show the visual effects of the blend mode

    If you are setting a blend mode can you show more of what is going on in the drawRect method?