GPUImage提供了GPUImageVideoCamera这么一个类,它的对象可以调用摄像头,而且加上滤镜的效果。ide
//init VideoCameraspa
//这里的两个参数能够设定拍摄录像的像素,还有拍摄录像的先后摄像头。不过要注意的是先后摄像头对像素的要求不一样,1080P的录像就不可能在钱摄像头完成了哈3d
videoCamera = [[GPUImageVideoCameraalloc] initWithSessionPreset:AVCaptureSessionPreset640x480cameraPosition:AVCaptureDevicePositionBack];对象
//这个参数是肯定摄像的方向blog
videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;接口
//开始摄像。这个方法能够放在任意地方,只要运行了这个方法就开始摄像了get
[videoCamerastartCameraCapture];it
这些只是开始摄像的初始化工做,若是没有图像输出那么也是没有用的哈(虽然StartCameraCapture的做用是开启摄像头,可是若是摄像头上的图像没有传递到屏幕上那么是没有图像的)io
//把滤镜效果加给摄像头class
[videoCameraaddTarget:testFilter];
//把摄像头上的图像给GPUImageView显示出来
[testFilteraddTarget:imageView];
在这里的TestFilter已是初始化过了的滤镜效果。
这样就能够成功摄像了
//关闭摄像头
[videoCamerastopCameraCapture];
关闭摄像头也很简单,就很少说了哈
还有一个最重要的地方,开启摄像头须要完成如下接口才能正常运行:
#pragma mark - vidoe camera
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
// Map UIDeviceOrientation to UIInterfaceOrientation.
UIInterfaceOrientation orient = UIInterfaceOrientationPortrait;
switch ([[UIDevicecurrentDevice] orientation])
{
caseUIDeviceOrientationLandscapeLeft:
orient = UIInterfaceOrientationLandscapeLeft;
break;
caseUIDeviceOrientationLandscapeRight:
orient = UIInterfaceOrientationLandscapeRight;
break;
caseUIDeviceOrientationPortrait:
orient = UIInterfaceOrientationPortrait;
break;
caseUIDeviceOrientationPortraitUpsideDown:
orient = UIInterfaceOrientationPortraitUpsideDown;
break;
caseUIDeviceOrientationFaceUp:
caseUIDeviceOrientationFaceDown:
caseUIDeviceOrientationUnknown:
// When in doubt, stay the same.
orient = fromInterfaceOrientation;
break;
}
videoCamera.outputImageOrientation = orient;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
returnYES; // Support all orientations.
}