使用AVFoundation框架,
首先声明以下对象:
#import "CustomeCameraViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>
@interface CustomeCameraViewController ()<AVCaptureFileOutputRecordingDelegate>
{
// AVCaptureSession对象来执行输入设备和输出设备之间的数据传递
AVCaptureSession *iSession;
//当前设备
AVCaptureDevice *iDevice;
//输入设备
AVCaptureDeviceInput *iDeviceInput;
//照片输出流
AVCaptureStillImageOutput *iStillImageOutput;
//预览图层
AVCaptureVideoPreviewLayer *iPreviewLayer;
}
初始化各对象:
- (void)viewDidLoad {
[super viewDidLoad];
//点击屏幕对焦
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(focusTap:)];
[self.view addGestureRecognizer:tap];
iSession = [[AVCaptureSession alloc]init];
NSArray *deviceArray = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in deviceArray) {
//AVCaptureDevicePositionBack 后置摄像头
//AVCaptureDevicePositionFront 前置摄像头
if (device.position == AVCaptureDevicePositionBack) {
iDevice = device;
}
}
iSession.sessionPreset = [self getSessionPresetForDevice:iDevice];
iDeviceInput = [[AVCaptureDeviceInput alloc]initWithDevice:iDevice error:nil];
输出设置。AVVideoCodecJPEG 输出jpeg格式图片
iStillImageOutput = [[AVCaptureStillImageOutput alloc]init];
NSDictionary *outputDic = [NSDictionary dictionaryWith