标签:
PBJVision
PBJVision, 是一个iOS相机操作的封装库,可以让你的应用快速简单地继承相机相关功能.
项目主页: PBJVision
最新示例:点击下载
注意: 示例需要在真机上运行,保存的视频在 tmp文件夹,可通过 ifunbox等第三方工具查看.
pod ‘PBJVision‘
导入头文件.
#import "PBJVision.h"
使用 [[PBJVision sharedInstance] previewLayer] 初始化相机预览视图.
// 预览视图和视频图层.
_previewView = [[UIView alloc] initWithFrame:CGRectZero];
_previewView.backgroundColor = [UIColor blackColor];
CGRect previewFrame = CGRectMake(0, 60.0f, CGRectGetWidth(self.view.frame), CGRectGetWidth(self.view.frame));
_previewView.frame = previewFrame;
_previewLayer = [[PBJVision sharedInstance] previewLayer];
_previewLayer.frame = _previewView.bounds;
_previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[_previewView.layer addSublayer:_previewLayer];
初始化并配置PBJVision控制器,然后开始使用相机预览功能.
- (void)_setup
{
_longPressGestureRecognizer.enabled = YES;
PBJVision *vision = [PBJVision sharedInstance];
vision.delegate = self;
vision.cameraMode = PBJCameraModeVideo;
vision.cameraOrientation = PBJCameraOrientationPortrait;
vision.focusMode = PBJFocusModeContinuousAutoFocus;
vision.outputFormat = PBJOutputFormatSquare;
[vision startPreview];
}
开始/中止/重新开始 录制.
- (void)_handleLongPressGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
switch (gestureRecognizer.state) {
case UIGestureRecognizerStateBegan:
{
if (!_recording)
[[PBJVision sharedInstance] startVideoCapture];
else
[[PBJVision sharedInstance] resumeVideoCapture];
break;
}
case UIGestureRecognizerStateEnded:
case UIGestureRecognizerStateCancelled:
case UIGestureRecognizerStateFailed:
{
[[PBJVision sharedInstance] pauseVideoCapture];
break;
}
default:
break;
}
}
结束录制.
[[PBJVision sharedInstance] endVideoCapture];
处理最终的视频输出或相应地错误.
- (void)vision:(PBJVision *)vision capturedVideo:(NSDictionary *)videoDict error:(NSError *)error
{
if (error && [error.domain isEqual:PBJVisionErrorDomain] && error.code == PBJVisionErrorCancelled) {
NSLog(@"退出录制");
return;
} else if (error) {
NSLog(@"视频录制时发生错误 (%@)", error);
return;
}
_currentVideo = videoDict;
NSString *videoPath = [_currentVideo objectForKey:PBJVisionVideoPathKey];
[_assetLibrary writeVideoAtPathToSavedPhotosAlbum:[NSURL URLWithString:videoPath] completionBlock:^(NSURL *assetURL, NSError *error1) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"视频已保存!" message: @"保存到缓存目录tmp中."
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"好", nil];
[alert show];
}];
}
设置 `PBJVision`控制器的属性可以设置最大视频录制时间:
[[PBJVision sharedInstance] setMaximumCaptureDuration:CMTimeMakeWithSeconds(5, 600)]; // 最多5秒.
可以通过 PBJVision 控制器的属性来设置视频的质量和压缩比特率.
@property (nonatomic, copy) NSString *captureSessionPreset;
@property (nonatomic) CGFloat videoBitRate;
@property (nonatomic) NSInteger audioBitRate;
@property (nonatomic) NSDictionary *additionalCompressionProperties;
标签:
原文地址:http://www.cnblogs.com/ios122/p/4761260.html