سؤال

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