自定义相机视图切换相机前面

时间:2015-01-14 12:08:09

标签: ios objective-c xcode

我正在为我的应用使用自定义相机视图。但是当我改变我的观点时它的反应不好我无法看到我的观点无论其停止显示实时视图的任何变化如果我不改变它的工作就好了,我不知道我尝试了很多我正在做什么错误或遗漏某些东西。我的代码是

-(void)abdbcam
{
    session=[[AVCaptureSession alloc]init];
    [session setSessionPreset:AVCaptureSessionPresetPhoto];
    
    /* AVCaptureDevice *inputDevice=[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
     NSError *error;
     AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&error];
     if([session canAddInput:deviceInput])
     {
     [session addInput:deviceInput];
     
     }*/
    NSArray *devices = [AVCaptureDevice devices];
    AVCaptureDevice *frontcamera;
    AVCaptureDevice *backcamera;
    
    for (AVCaptureDevice *device in devices)
    {
        NSLog(@"Device Nmae %@",[device localizedName]);
        
        if ([device hasMediaType:AVMediaTypeVideo])
        {
            if ([device position] == AVCaptureDevicePositionBack)
            {
                NSLog(@"device position: bAck");
                backcamera = device;
            }
            else
            {
                
                NSLog(@"device position : front");
                frontcamera = device;
            }
        }
    }
    if (!FrontCamera)
    {
        NSError *error = nil;
        AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:backcamera error:&error];
        if (!input)
        {
            NSLog(@"error: trying to open camera: %@", error);
            
        }
        [session addInput:input];
    }
    if (FrontCamera) {
        NSError *error = nil;
        AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:frontcamera error:&error];
        if (!input) {
            NSLog(@"error: trying to open camera: %@", error);
            
        }
        [session addInput:input];
        
    }
    
    AVCaptureVideoPreviewLayer *previewLayer=[[AVCaptureVideoPreviewLayer alloc]initWithSession:session];
    [previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    CALayer *rootLayer = [[self view]layer];
    [rootLayer setMasksToBounds:YES];
    CGRect frame=self.camerarole.frame;
    
    
    [previewLayer setFrame:frame];
    
    [rootLayer insertSublayer:previewLayer atIndex:0];
    
    stillImageOutput=[[AVCaptureStillImageOutput alloc]init];
    
    NSDictionary *outputSetting=[[NSDictionary alloc]initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey, nil];
    [stillImageOutput setOutputSettings:outputSetting];
    [session addOutput:stillImageOutput];
    [session startRunning];
}

// now my segment controller code is

- (IBAction)switchcamera:(id)sender {
    if (cameraswitch.selectedSegmentIndex == 0) {
        FrontCamera = YES;
        [self abdbcam];
    }
    if (cameraswitch.selectedSegmentIndex == 1) {
        [self abdbcam];
    }

如果我有任何错误,请看看并建议我

2 个答案:

答案 0 :(得分:0)

所以我已经实现了自定义相机切换。请检查以下代码:

isUsingFrontFacingCamera是一个bool标志,也会检测你当时正在使用哪个相机

- (void) switchCam{

    if (![self cameraIsRunning])
        return;

    AVCaptureDevicePosition desiredPosition;

    if (isUsingFrontFacingCamera)
        desiredPosition = AVCaptureDevicePositionBack;
    else
        desiredPosition = AVCaptureDevicePositionFront;

    for (AVCaptureDevice *d in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {

        if ([d position] == desiredPosition) {

            [[captureVideoPreviewLayer session] beginConfiguration];

            AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:d error:nil];

            for (AVCaptureInput *oldInput in [[captureVideoPreviewLayer session] inputs]) {
                [[captureVideoPreviewLayer session] removeInput:oldInput];
            }

            [[captureVideoPreviewLayer session] addInput:input];
            [[captureVideoPreviewLayer session] commitConfiguration];

            break;
        }
    }

    isUsingFrontFacingCamera = !isUsingFrontFacingCamera;
}

答案 1 :(得分:0)

此代码适用于我正在工作的时间。我发布这个是因为它对任何人都有帮助。

NSArray *inputs = 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
        [session beginConfiguration];

        [session removeInput:input];
        [session addInput:newInput];

        // Changes take effect once the outermost commitConfiguration is invoked.
        [session commitConfiguration];
        break;
    }
}


- (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition)position
 {
   NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    for ( AVCaptureDevice *device in devices )
    if ( device.position == position )
        return device;
    return nil;
 }