相机属于系统硬件,这就需要我们来手动调用iPhone的相机硬件,分为以下步骤:
2016-03-30 上午9.51.09.png
1、首先声明以下对象
#import
//捕获设备,通常是前置摄像头,后置摄像头,麦克风(音频输入)
@property
(
nonatomic
,
strong
)
AVCaptureDevice
*device;
//AVCaptureDeviceInput 代表输入设备,他使用AVCaptureDevice 来初始化
@property
(
nonatomic
,
strong
)
AVCaptureDeviceInput
*input;
//输出图片
@property
(
nonatomic
,
strong
)
AVCaptureStillImageOutput
*imageOutput;
//session:由他把输入输出结合在一起,并开始启动捕获设备(摄像头)
@property
(
nonatomic
,
strong
)
AVCaptureSession
*session;
//图像预览层,实时显示捕获的图像
@property
(
nonatomic
,
strong
)
AVCaptureVideoPreviewLayer
*previewLayer;
2、初始化各个对象
- (
void
)cameraDistrict
{
// AVCaptureDevicePositionBack 后置摄像头
// AVCaptureDevicePositionFront 前置摄像头
self
.device
= [
self
cameraWithPosition:
AVCaptureDevicePositionFront
];
self
.input
= [[
AVCaptureDeviceInput
alloc] initWithDevice:
self
.device
error:
nil
];
self
.imageOutput
= [[
AVCaptureStillImageOutput
alloc] init];
self
.session
= [[
AVCaptureSession
alloc] init];
// 拿到的图像的大小可以自行设定
// AVCaptureSessionPreset320x240
// AVCaptureSessionPreset352x288
// AVCaptureSessionPreset640x480
// AVCaptureSessionPreset960x540
// AVCaptureSessionPreset1280x720
// AVCaptureSessionPreset1920x1080
// AVCaptureSessionPreset3840x2160
self
.session
.sessionPreset
=
AVCaptureSessionPreset640x480
;
//输入输出设备结合
if
([
self
.session
canAddInput:
self
.input
]) {
[
self
.session
addInput:
self
.input
];
}
if
([
self
.session
canAddOutput:
self
.imageOutput
]) {
[
self
.session
addOutput:
self
.imageOutput
];
}
//预览层的生成
self
.previewLayer
= [[
AVCaptureVideoPreviewLayer
alloc] initWithSession:
self