3

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()
    }
}
Stanislav Smida
  • 1,565
  • 17
  • 23

1 Answers1

2

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?

Iain Smith
  • 9,230
  • 4
  • 50
  • 61
  • I'm doing something similar and still having issues after adding the above. I'm using a CIAffineTransform filter to rotate a CIImage and getting the black background. My CIContext is created from an EAGLContext, and I'm drawing the final image into a CVPixelBuffer that's eventually rendered as a video frame. I've added the above OpenGL calls, but the area outside of the rotated image is still black. Any thoughts as to other settings I might need to make the background transparent? Thanks! – Clay Garrett Dec 10 '16 at 07:07