ios开发之设备前后摄像头切换

Bigheart 2013-07-20

#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;
        }
    } 
}

相关推荐

Enjoyendless / 0评论 2020-06-11

CoderChang / 0评论 2014-06-04