ios开发之设备前后摄像头切换
#import <AVFoundation/AVFoundation.h> // Switching between front and back cameras - (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition)position { NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; for ( AVCaptureDevice *device in devices ) if ( device.position == position ) return device; return nil; } - (void)swapFrontAndBackCameras { // Assume the session is already running NSArray *inputs = self.session.inputs; for ( AVCaptureDeviceInput *input in inputs ) { AVCaptureDevice *device = input.device; if ( [device hasMediaType:AVMediaTypeVideo] ) { AVCaptureDevicePosition position = device.position; AVCaptureDevice *newCamera = nil; AVCaptureDeviceInput *newInput = nil; if (position == AVCaptureDevicePositionFront) newCamera = [self cameraWithPosition:AVCaptureDevicePositionBack]; else newCamera = [self cameraWithPosition:AVCaptureDevicePositionFront]; newInput = [AVCaptureDeviceInput deviceInputWithDevice:newCamera error:nil]; // beginConfiguration ensures that pending changes are not applied immediately [self.session beginConfiguration]; [self.session removeInput:input]; [self.session addInput:newInput]; // Changes take effect once the outermost commitConfiguration is invoked. [self.session commitConfiguration]; break; } } }
相关推荐
Bigheart 2013-07-20
lcqin 2020-08-11
yaodilu 2020-08-03
lanzhusiyu 2020-07-18
小仙儿 2020-06-25
xiaohuli 2020-06-11
CaiKanXP 2020-06-09
songfens 2020-06-08
CSSEIKOCS 2020-05-19
aSuncat 2020-05-10
Phoebe的学习天地 2020-05-09
nicepainkiller 2020-05-05
amwayy 2020-05-01
AlisaClass 2020-04-14
coulder 2020-04-08
wangjie 2020-02-26
xiaohuli 2020-02-23