Switch Camera Front or Rear[iPhone Source]
// 버튼과 연결 시킬 메소드
-(IBAction)CameraSwap:(id)sender
{
// 세션이 실행되고 있어야 한다는 가정
NSArray *Current_inputs = self._captureSession.inputs;
for ( AVCaptureDeviceInput *old_input in Current_inputs) {
AVCaptureDevice *device = old_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];
// 설정 시작
[self._captureSession beginConfiguration];
[self._captureSession removeInput:old_input];
[self._captureSession addInput:newInput];
[self._captureSession commitConfiguration];
break;
}
}
}
// 이 메소드도 헤더 파일에 등록해줘야 한다.
-(AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition)position {
NSArray *Camera_devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in Camera_devices) {
if (device.position == position) {
return device;
}
}
return nil;
}
댓글
댓글 쓰기