2018-07-04 有用的代码
实现摄像头反转的代码,以后可能会用到着
- (void)swapFrontAndBackCameras:(AVCaptureSession*)mySession{
NSArray*inputs =mySession.inputs;
for(AVCaptureDeviceInput*inputininputs ) {
AVCaptureDevice*device = input.device;
if ( [device hasMediaType:AVMediaTypeVideo] ) {
AVCaptureDevicePositionposition = 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];
[mySessionbeginConfiguration];
[mySessionremoveInput:input];
[mySessionaddInput:newInput];
[mySessioncommitConfiguration];
break;
}
}
}
- (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition)position{
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for(AVCaptureDevice*deviceindevices )
if( device.position== position )
returndevice;
return nil;
}
//打开和关闭闪光灯
- (void)flashSwitch:(UIButton*)button {
if(self.input!=nil) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if([devicehasTorch] && [devicehasFlash]){
[devicelockForConfiguration:nil];
BOOLselected = [buttonisSelected];
AVCaptureFlashModemodel = device.flashMode;
if(model !=AVCaptureFlashModeOff) {
[devicesetTorchMode:AVCaptureTorchModeOff];
[devicesetFlashMode:AVCaptureFlashModeOff];
}
else{
[devicesetTorchMode:AVCaptureTorchModeOn];
[devicesetFlashMode:AVCaptureFlashModeOn];
}
button.selected= !selected;
[deviceunlockForConfiguration];
}
}
}
//获取一定尺寸的宽度和高度
#define COMPONENT_WIDTH(width) (width * SCREEN_WIDTH /750.0)
#define COMPONENT_HEIGHT(height) (height * SCREEN_HEIHGT /1334.0)