//
//  ViewController.m
//  AVFountionCamera
//
//  Created by ZhuYi on 16/5/3.
//  Copyright © 2016年 DDP. All rights reserved.
//

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>

@interface ViewController (){
    //负责输入和输出设置之间的数据传递
    AVCaptureSession *captureSesstion;
    //负责从AVCaptureDevice获得输入数据
    AVCaptureDeviceInput *capturedeviceInput;
    //照片输出流
    AVCaptureStillImageOutput *captureStillImageOutput;
    //相机拍摄预览图层
    AVCaptureVideoPreviewLayer *captureVediopreviewLayer;
    //拍照按钮
    UIButton *takephotoButton;
    UIView *viewContainer;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    viewContainer = [[UIView alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width, self.view.frame.size.height - )];
    [self.view addSubview:viewContainer];
    takephotoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    takephotoButton.backgroundColor = [UIColor orangeColor];
    [takephotoButton addTarget:self action:@selector(takePhoto) forControlEvents:UIControlEventTouchUpInside];
    takephotoButton.frame = CGRectMake(, self.view.frame.size.height - , self.view.frame.size.width, );
    [self.view addSubview:takephotoButton];
    //初始化会话
    captureSesstion = [[AVCaptureSession alloc] init];
    if ([captureSesstion canSetSessionPreset:AVCaptureSessionPreset1920x1080]) {
        captureSesstion.sessionPreset = AVCaptureSessionPreset1920x1080;
    }
    //获得输入设备
    AVCaptureDevice *captureDevice=[self getCameraDeviceWithPosition:AVCaptureDevicePositionBack];//取得后置摄像头
    if (!captureDevice) {
        NSLog(@"取得后置摄像头时出现问题.");
        return;
    }
    NSError *error=nil;
    //根据输入设备初始化设备输入对象,用于获得输入数据
    capturedeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:captureDevice error:&error];
    if (error) {
        NSLog(@"取得设备输入对象时出错,错误原因:%@",error.localizedDescription);
        return;
    }
    //初始化设备输出对象,用于获得输出数据
    captureStillImageOutput=[[AVCaptureStillImageOutput alloc]init];
    NSDictionary *outputSettings = @{AVVideoCodecKey:AVVideoCodecJPEG};
    [captureStillImageOutput setOutputSettings:outputSettings];//输出设置

    //将设备输入添加到会话中
    if ([captureSesstion canAddInput:capturedeviceInput]) {
        [captureSesstion addInput:capturedeviceInput];
    }

    //将设备输出添加到会话中
    if ([captureSesstion canAddOutput:captureStillImageOutput]) {
        [captureSesstion addOutput:captureStillImageOutput];
    }

    //创建视频预览层,用于实时展示摄像头状态
    captureVediopreviewLayer=[[AVCaptureVideoPreviewLayer alloc]initWithSession:captureSesstion];

    CALayer *layer = viewContainer.layer;
    layer.masksToBounds=YES;

    captureVediopreviewLayer.frame=layer.bounds;
    captureVediopreviewLayer.videoGravity=AVLayerVideoGravityResizeAspectFill;//填充模式
    //将视频预览层添加到界面中
    [layer addSublayer:captureVediopreviewLayer];
    [captureSesstion startRunning];
}
-(AVCaptureDevice *)getCameraDeviceWithPosition:(AVCaptureDevicePosition )position{
    NSArray *cameras= [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    for (AVCaptureDevice *camera in cameras) {
        if ([camera position]==position) {
            return camera;
        }
    }
    return nil;
}
- (void)takePhoto{
    //根据设备输出获得连接
    AVCaptureConnection *captureConnection=[captureStillImageOutput connectionWithMediaType:AVMediaTypeVideo];
    //根据连接取得设备输出的数据
    [captureStillImageOutput captureStillImageAsynchronouslyFromConnection:captureConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
        if (imageDataSampleBuffer) {
            NSData *imageData=[AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
            UIImage *image=[UIImage imageWithData:imageData];
            UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
        }

    }];
}
@end

AVFoundation--AVCaptureSession的更多相关文章

  1. &lt;图形图像,动画,多媒体&gt; 读书笔记 --- 录制与编辑视频

    使用UIImagePickerController 进行录制 #import "ViewController.h" #import <MobileCoreServices/M ...

  2. 用AVFoundation自定义相机拍照

    自定义拍照或者录视频的功能,就需要用到AVFoundation框架,目前我只用到了拍照,所以记录下自定义拍照用法,视频用法等用上了再补充,应该是大同小异 demo在这里:https://github. ...

  3. iOS开发--AVFoundation自定义相机

    首先导入一个头文件 #import <AVFoundation/AVFoundation.h> 由于后面我们需要将拍摄好的照片写入系统相册中,所以我们在这里还需要导入一个相册需要的头文件 ...

  4. iOS使用AVFoundation实现二维码扫描(ios7以上)——转载

    关于二维码扫描有不少优秀第三方库: ZBar SDK 里面有详细的文档,相应介绍也非常多,如:http://rdcworld-iphone.blogspot.in/2013/03/how-to-use ...

  5. iOS使用AVFoundation实现二维码扫描

    原文:http://strivingboy.github.io/blog/2014/11/08/scan-qrcode/ 关于二维码扫描有不少优秀第三方库如: ZBar SDK 里面有详细的文档,相应 ...

  6. AVFoundation的使用

    AVFoundation的使用 2013-05-03 14:50:21|  分类: iphone_dev_note|举报|字号 订阅         相机相关应用一般会用到AVFoundation. ...

  7. AVFoundation视频流处理

    框架 首先我们从整体对所需框架做个初步了解. AVFoundation在相关框架栈中的的位置: 为了捕捉视频,我们需要这样几种类(与其它的子类). AVCaptureDevice 代表了输入设备,例如 ...

  8. 使用AVCaptureSession捕捉视频

    #import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> #import <AssetsLibrary/ ...

  9. 使用AVCaptureSession捕捉静态图片

    #import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> #import <AssetsLibrary/ ...

  10. 使用AVCaptureSession显示相机预览

    #import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> @interface ViewController ...

随机推荐

  1. ext3学习小结

    先介绍一下ext3和ext4的一些区别吧,初看ext4相对于ext3源码还是有很多不同的,ext4加入的define和create两个强大的类,ext4为了让源码容易看,特意将所有的类进行了defin ...

  2. Netty(7)源码-ByteBuf

    一.ByteBuf工作原理 1. ByteBuf是ByteBuffer的升级版: jdk中常用的是ByteBuffer,从功能角度上,ByteBuffer可以完全满足需要,但是有以下缺点: ByteB ...

  3. CoreJavaE10V1P3.4 第3章 Java的基本编程结构-3.4 变量

    1.在Java中,每一个变量都必须有一个类型,在变量声明是,类型必须在变量名之前.示例如下: double salary; int vacationDays; long earthPopulation ...

  4. Winform_devexpress开发框架主界面设计

    做了好多年的C#开发,从.Net.Winform及第三方的DevExpress.无论什么样的系统,主界面的设计及风格无疑非常重要.从客户的角度考虑,要求功能区清晰,整体美观大方,这样才会有可能从第一视 ...

  5. 分布式缓存Memcached/memcached/memcache详解及区别

    先来解释下标题中的三种写法:首字母大写的Memcached,指的是Memcached服务器,就是独立运行Memcached的后台服务器,用于存储缓存数据的“容器”.memcached和memcache ...

  6. Python 代码规范

    命名 module_name, package_name, ClassName, method_name, ExceptionName, function_name, GLOBAL_VAR_NAME, ...

  7. [SOJ]连通性问题

    Description 关系R具有对称性和传递性.数对p q表示pRq,p和q是0或自然数,p不等于q.要求写一个程序将数对序列进行过滤,如果一个数对可以通过前面数对的传递性得到,则将其滤去.例如:输 ...

  8. 使用eclipse XML catalog绑定dtd文件

    有时候我们想编辑struts或spring的xml配置文件的时候,输了“<”之后eclipse却没有提示关键字,这是因为eclipse需要到网络下载dtd文件而由于网络原因没下载下来所以无法提示 ...

  9. MySql 使用教程(摘自网络)

    一.连接 mysql    格式:mysql-h 主机地址 -u 用户名 p 用户密码    1 例 1 连接到本机上的 mysql    首先在打开 DOS 窗口,然后进入目录 mysql\bin ...

  10. jsp日期插件My97DatePicker 强大的日期控件 使用方便简单

    本文属转载(希望对编程爱好者有所帮助)详情请访问官方网站 http://www.my97.net/dp/index.asp 一. 简介 1. 简介 目前的版本是:4.7 2. 注意事项 My97Dat ...