码迷,mamicode.com
首页 > 其他好文 > 详细

PBJVision 快速在应用中集成相机/拍摄功能

时间:2015-08-26 19:39:05      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

技术分享PBJVision

简介

PBJVision, 是一个iOS相机操作的封装库,可以让你的应用快速简单地继承相机相关功能.

项目主页: PBJVision

最新示例:点击下载

注意: 示例需要在真机上运行,保存的视频在 tmp文件夹,可通过 ifunbox等第三方工具查看.

快速入门

安装

通过 CocoaPods 安装

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;

PBJVision 快速在应用中集成相机/拍摄功能

标签:

原文地址:http://www.cnblogs.com/ios122/p/4761260.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!