美文网首页
2018-07-04 有用的代码

2018-07-04 有用的代码

作者: 北你妹的风 | 来源:发表于2018-07-05 13:25 被阅读3次

    实现摄像头反转的代码,以后可能会用到着

    - (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)

    相关文章

      网友评论

          本文标题:2018-07-04 有用的代码

          本文链接:https://www.haomeiwen.com/subject/rfcpuftx.html