如何从GPUImage相机获取UIImage?

时间:2014-07-13 17:57:02

标签: ios gpuimage

我知道我可以使用静态相机的capturePhotoAsImageProcessedUpToFilter方法,但它会发出快门咔嗒声,而我仍然需要进行一些处理,我还不想让它发出声音。

我尝试使用过滤器的imageFromCurrentFramebuffer方法,但这总是变为零。

CGRect frame = CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width);
_backgroundImageView = [[GPUImageView alloc] initWithFrame:frame];
[self.view insertSubview:_backgroundImageView atIndex:0];

_camera = [[GPUImageStillCamera alloc] initWithSessionPreset:AVCaptureSessionPresetHigh cameraPosition:AVCaptureDevicePositionBack];
_camera.outputImageOrientation = UIInterfaceOrientationLandscapeRight;
filter = [[GPUImageBrightnessFilter alloc] init];
filter.brightness = 0;

[_camera addTarget: filter];

[filter addTarget:_backgroundImageView];
[_camera startCameraCapture];
// processTimer is to periodically do processing on what's being shown on screen.
processTimer =[NSTimer timerWithTimeInterval:10 target:self selector:@selector(processImage) userInfo:nil repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:processTimer forMode:NSRunLoopCommonModes];


-(void) processImage {
    [processTimer invalidate];

    UIImage *testImage = [filter imageFromCurrentFramebuffer]; // always nil

    [_camera capturePhotoAsImageProcessedUpToFilter:filter withCompletionHandler:^(UIImage *processedImage, NSError *error) {

         // additional processing here ... don't want shutter sound yet.

    }];
}

2 个答案:

答案 0 :(得分:0)

您可以使用以下方法从链中的任何过滤器获得没有快门声的UIImage:

- (UIImage *)imageFromCurrentFramebuffer

GPUImageOutput及其子类的

GPUImageVideoCameraGPUImageStillCameraGPUImageFilter等。

如果您的目标是显示视口,请使用GPUImageView作为过滤器链中的最后一项。

答案 1 :(得分:0)

好的..终于得到了问题的答案。我需要在调用imageFromCurrentFramebuffer之前在过滤器上调用useNextFrameForImageCapture。这有时仍然会返回零,但是我得到了我需要的东西。