Search code examples
iosobjective-cuiviewgpuimagegpuimagestillcamera

GPUImageStillCamera is not working


I have a UIViewController which has a child hierarchy like this
enter image description here

and then, I use this code to show camera preview

stillCamera = [[GPUImageStillCamera alloc] init];
stillCamera.outputImageOrientation = UIInterfaceOrientationPortrait;

filter = [[GPUImageStretchDistortionFilter alloc] init];
[filter prepareForImageCapture];
[stillCamera addTarget:filter];
GPUImageView *filterview = [[GPUImageView alloc] init];
_preview = filterview;
[filter addTarget:filterview];

[stillCamera startCameraCapture];

variable _preview is a UIView whose class is set to "GPUImageView". On the hierarchy image above, it is the selected one.

When I showed the view, it showed nothing on _preview. Just a blank UIView. What did I do wrong?

When I changed the line _preview = filterview to self.view = filterview, it worked as expected. Does it have to be self.view?


Solution

  • I think the problem with your above code is in the lines:

    GPUImageView *filterview = [[GPUImageView alloc] init];
    _preview = filterview;
    

    If you've defined your GPUImageView in Interface Builder, _preview is going to pointing to a property that you've associated with that view in IB. The above code replaces that pointer with a view that's not the one defined in IB. Therefore, your visible view will never get your camera content.

    You need to instead remove those two lines and use

    [filter addTarget:_preview];
    

    making sure that your above setup comes after your view controller has had a chance to properly associate your outlets with your NIB and that the view in IB is indeed a GPUImageView.