- (void)viewDidLoad {
[super viewDidLoad];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 100, 100);
btn.center = self.view.center;
[btn setTitle:@"拍照" forState:UIControlStateNormal];
btn.backgroundColor = [UIColor orangeColor];
[btn addTarget:self action:@selector(clickAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
//会话
_sesion = [[AVCaptureSession alloc] init];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
[_sesion setSessionPreset:AVCaptureSessionPresetPhoto];
}
AVCaptureDevice *device = [self cameraWithPosition: AVCaptureDevicePositionBack];
//输入源
_input = [[AVCaptureDeviceInput alloc] initWithDevice:device error:nil];
if ([_sesion canAddInput:_input]) {
[_sesion addInput:_input];
}
//输出源
_photoOut = [AVCaptureStillImageOutput new];
if ([_sesion canAddOutput:_photoOut]) {
[_sesion addOutput:_photoOut];
}
_previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_sesion];
_previewLayer.bounds = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
[self.view.layer addSublayer:_previewLayer];
[_sesion startRunning];
}
- (void)clickAction {
AVCaptureConnection *connect = [_photoOut connectionWithMediaType:AVMediaTypeVideo];
[self.photoOut captureStillImageAsynchronouslyFromConnection:connect completionHandler:^(CMSampleBufferRef _Nullable imageDataSampleBuffer, NSError * _Nullable error) {
NSData *data = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [UIImage imageWithData:data];
UIImageView *img = [[UIImageView alloc] initWithImage:image];
img.frame = CGRectMake(20, 20, 100, 100);
[self.view addSubview:img];
}];
}
- (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition)position
{
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for ( AVCaptureDevice *device in devices ){
if ( device.position == position )
return device;
}
return nil;
}
iOS相机
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1.设置提示框 sheet代理 接上图 代理 如果想要裁剪后的图片切记这么写 *UIImagePickerCont...