iOS自定义照相机

  • 关于iOS调用摄像机来获取照片,通常我们都会调用UIImagePickerController来调用系统提供的相机来拍照,这个控件非常好用。但是有时UIImagePickerController控件无法满足我们的需求,例如我们需要更加复杂的OverlayerView,这时候我们就要自己构造一个摄像机控件了。

    这需要使用AVFoundation.framework这个framework里面的组件了,所以我们先要导入<AVFoundation/AVFoundation.h>这个头文件,另外还需要的组件官方文档是这么说的:

    ● An instance of AVCaptureDevice to represent the input device, such as a camera or microphone
    ● An instance of a concrete subclass of AVCaptureInput to configure the ports from the input device
    ● An instance of a concrete subclass of AVCaptureOutput to manage the output to a movie file or still image
    ● An instance of AVCaptureSession to coordinate the data flow from the input to the output


    这里我只构造了一个具有拍照功能的照相机,至于录影和录音功能这里就不加说明了。

    总结下来,我们需要以下的对象:

    01. @property (nonatomic, strong)       AVCaptureSession            * session;
    02. //AVCaptureSession对象来执行输入设备和输出设备之间的数据传递
    03. @property (nonatomic, strong)       AVCaptureDeviceInput        * videoInput;
    04. //AVCaptureDeviceInput对象是输入流
    05. @property (nonatomic, strong)       AVCaptureStillImageOutput   * stillImageOutput;
    06. //照片输出流对象,当然我的照相机只有拍照功能,所以只需要这个对象就够了
    07. @property (nonatomic, strong)       AVCaptureVideoPreviewLayer  * previewLayer;
    08. //预览图层,来显示照相机拍摄到的画面
    09. @property (nonatomic, strong)       UIBarButtonItem             * toggleButton;
    10. //切换前后镜头的按钮
    11. @property (nonatomic, strong)       UIButton                    * shutterButton;
    12. //拍照按钮
    13. @property (nonatomic, strong)       UIView                      * cameraShowView;
    14. //放置预览图层的View 

    我的习惯是在init方法执行的时候创建这些对象,然后在viewWillAppear方法里加载预览图层。现在就让我们看一下代码就清楚了。

    01. - (void) initialSession
    02. {
    03. //这个方法的执行我放在init方法里了
    04. self.session = [[AVCaptureSession alloc] init];
    05. self.videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self frontCamera] error:nil];
    06. //[self fronCamera]方法会返回一个AVCaptureDevice对象,因为我初始化时是采用前摄像头,所以这么写,具体的实现方法后面会介绍
    07. self.stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
    08. NSDictionary * outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey, nil];
    09. //这是输出流的设置参数AVVideoCodecJPEG参数表示以JPEG的图片格式输出图片
    10. [self.stillImageOutput setOutputSettings:outputSettings];
    11.  
    12. if ([self.session canAddInput:self.videoInput]) {
    13. [self.session addInput:self.videoInput];
    14. }
    15. if ([self.session canAddOutput:self.stillImageOutput]) {
    16. [self.session addOutput:self.stillImageOutput];
    17. }
    18.  
    19. }
    这是获取前后摄像头对象的方法

    01. - (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition) position {
    02. NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    03. for (AVCaptureDevice *device in devices) {
    04. if ([device position] == position) {
    05. return device;
    06. }
    07. }
    08. return nil;
    09. }
    10.  
    11.  
    12. - (AVCaptureDevice *)frontCamera {
    13. return [self cameraWithPosition:AVCaptureDevicePositionFront];
    14. }
    15.  
    16. - (AVCaptureDevice *)backCamera {
    17. return [self cameraWithPosition:AVCaptureDevicePositionBack];
    18. }

    接下来在viewWillAppear方法里执行加载预览图层的方法

    01. - (void) setUpCameraLayer
    02. {
    03. if (_cameraAvaible == NO) return;
    04.  
    05. if (self.previewLayer == nil) {
    06. self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
    07. UIView * view = self.cameraShowView;
    08. CALayer * viewLayer = [view layer];
    09. [viewLayer setMasksToBounds:YES];
    10.  
    11. CGRect bounds = [view bounds];
    12. [self.previewLayer setFrame:bounds];
    13. [self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspect];
    14.  
    15. [viewLayer insertSublayer:self.previewLayer below:[[viewLayer sublayers] objectAtIndex:0]];
    16.  
    17. }
    18. }

    注意以下的方法,在viewDidAppear和viewDidDisappear方法中启动和关闭session

    01. - (void) viewDidAppear:(BOOL)animated
    02. {
    03. [super viewDidAppear:animated];
    04. if (self.session) {
    05. [self.session startRunning];
    06. }
    07. }
    08.  
    09. - (void) viewDidDisappear:(BOOL)animated
    10. {
    11. [super viewDidDisappear: animated];
    12. if (self.session) {
    13. [self.session stopRunning];
    14. }
    15. }

    接着我们就来实现切换前后镜头的按钮,按钮的创建我就不多说了

    01. - (void)toggleCamera {
    02. NSUInteger cameraCount = [[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo] count];
    03. if (cameraCount > 1) {
    04. NSError *error;
    05. AVCaptureDeviceInput *newVideoInput;
    06. AVCaptureDevicePosition position = [[_videoInput device] position];
    07.  
    08. if (position == AVCaptureDevicePositionBack)
    09. newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self frontCamera] error:&error];
    10. else if (position == AVCaptureDevicePositionFront)
    11. newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self backCamera] error:&error];
    12. else
    13. return;
    14.  
    15. if (newVideoInput != nil) {
    16. [self.session beginConfiguration];
    17. [self.session removeInput:self.videoInput];
    18. if ([self.session canAddInput:newVideoInput]) {
    19. [self.session addInput:newVideoInput];
    20. [self setVideoInput:newVideoInput];
    21. else {
    22. [self.session addInput:self.videoInput];
    23. }
    24. [self.session commitConfiguration];
    25. else if (error) {
    26. NSLog(@"toggle carema failed, error = %@", error);
    27. }
    28. }
    29. }

    这是切换镜头的按钮方法

    01. - (void) shutterCamera
    02. {
    03. AVCaptureConnection * videoConnection = [self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo];
    04. if (!videoConnection) {
    05. NSLog(@"take photo failed!");
    06. return;
    07. }
    08.  
    09. [self.stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
    10. if (imageDataSampleBuffer == NULL) {
    11. return;
    12. }
    13. NSData * imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
    14. UIImage * image = [UIImage imageWithData:imageData];
    15. NSLog(@"image size = %@",NSStringFromCGSize(image.size));
    16. }];
    17. }
    这是拍照按钮的方法

    这样自定义照相机的简单功能就完成了,如果你想要再添加其他复杂的功能,可以参考一下下面这篇文章,希望对你们有所帮助。

    http://course.gdou.com/blog/Blog.pzs/archive/2011/12/14/10882.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值