문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

You almost have it, try this programmatically first, as I have a very similar situation happening, which is working:

  1. Make your camera:

    stillCamera = [[GPUImageStillCamera alloc] init];
    

    stillCamera.outputImageOrientation = UIInterfaceOrientationPortrait;

  2. Make your preview view:

    GPUImageView *previewView = [[GPUImageView alloc] initWithFrame:CGRectMake(x, y, w, h)];

  3. Make your filter:

    filter = [[GPUImageStretchDistortionFilter alloc] init];

  4. Now add your filter to your camera:

    [stillCamera addTarget:filter];

  5. Finally, add your video camera to your previewView:

    [stillCamera addTarget:previewView];

EDIT:

Totally goofed! You need to now add the previewView to the actual view! Try this:

[self.view addSubview:previewView];

Give that a try, GPUImage can be a little confusing at first, but with some tinkering it will work!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top