从后面切换到前面饰相机Xcode

时间:2014-08-31 01:53:43

标签: ios xcode camera

我正试图访问前置摄像头,我觉得我搞砸了。我试图访问其他问题such as this,但它没有成功。

以下是显示后置摄像头的代码:

-(void)viewWillAppear:(BOOL)animated{
    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];
    }

    AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    [previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    CALayer *rootLayer = [[self view] layer];
    [rootLayer setMasksToBounds:YES];
    CGRect frame = self.frameForCapture.frame;

    [previewLayer setFrame: frame];

    [rootLayer insertSublayer:previewLayer atIndex:0];

    stillImageOutput = [[AVCaptureStillImageOutput alloc] init];

    [session startRunning];
来自@urnotsam发布的Apple文档的

,我看到了:

AVCaptureDevicePosition
Constants to specify the position of a capture device.

typedef enum : NSInteger {
   AVCaptureDevicePositionUnspecified = 0,
   AVCaptureDevicePositionBack  = 1,
   AVCaptureDevicePositionFront = 2
} AVCaptureDevicePosition;

我尝试通过在AVCaptureDevice上面放入typedef并用前置摄像头替换defaultDeviceWithMediaType来编辑代码。那没用。

因此代码目前正在使用默认摄像头,后者是主要的,但我正在尝试让应用程序打开前置摄像头......感谢您的帮助到目前为止。

0 个答案:

没有答案