iOS捕获图像形式GPUImage

时间:2015-08-26 10:28:14

标签: ios xcode gpuimage image-capture

我正在尝试使用使用GPUImage的应用程序捕获图像。我把相机设置得像这样

self.videoCamera = [[GPUImageVideoCamera alloc]
                    initWithSessionPreset:AVCaptureSessionPresetHigh
                    cameraPosition:AVCaptureDevicePositionBack];
_videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
[_videoCamera startCameraCapture];
[_videoCamera addTarget:sample1ImageView];

我使用自定义过滤器:

radFilter = [[GPUImageCustomFilter alloc] init];
[_videoCamera addTarget:cusFilter];
[cusFilter addTarget:imageView];

然后我将此代码用于摄像头捕获:

[_videoCamera pauseCameraCapture];
[radFilter forceProcessingAtSize:CGSizeMake(600, 600)];
[radFilter useNextFrameForImageCapture];
UIImage* capturedImage = [radFilter imageFromCurrentFramebuffer];
UIImageWriteToSavedPhotosAlbum(capturedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
[_videoCamera resumeCameraCapture];

我得到的只是白色图片,rgb 0,0,0。 我尝试在IBAction和rac_signalForControlEvents中保存,我使用了调度但没有改变。谁能告诉我我做错了什么?

谢谢你, 亚历

2 个答案:

答案 0 :(得分:1)

尝试使用像这样的GPUImageStillCamera ..

你的.h文件中的

..

GPUImageStillCamera *stillCamera;
GPUImageView * filterView;
你的.m文件中的

是viewdidload ..

selectedFilter = [[GPUImageFilter alloc]init];
filterView=[[GPUImageView alloc]init];

stillCamera=[[GPUImageStillCamera alloc]initWithSessionPreset:AVCaptureSessionPresetPhoto cameraPosition:AVCaptureDevicePositionFront];
stillCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
[stillCamera addTarget:selectedFilter];
[selectedFilter addTarget:filterView];
[stillCamera startCameraCapture];

在用于捕获图像的UIButtons点击事件上做这些,我希望它有所帮助..

[stillCamera capturePhotoAsImageProcessedUpToFilter:selectedFilter withCompletionHandler:^(UIImage *processedImage, NSError *error)
 {
        UIImageWriteToSavedPhotosAlbum(processedImage, self, nil, nil);
 }];

答案 1 :(得分:1)

You can use this code to capture the image.

- (UIImage *) screenshot {
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);

    [self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

- (IBAction)btnCaptureClicked:(id)sender
{
    [videoCamera pauseCameraCapture];
    [filter useNextFrameForImageCapture];
    //[filter imageFromCurrentFramebuffer];
    UIImage *capturedImage= [self screenshot];
    if(capturedImage != nil)
    {
        UIImageWriteToSavedPhotosAlbum(capturedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }
    [videoCamera resumeCameraCapture];
}