我可以使用非模态显示的相机吗?怎么样?

时间:2011-10-19 19:23:03

标签: objective-c ios uiview uiimagepickercontroller

我正在开发一个应用程序,我从其他人手中接过了开发。他们呈现的相机在用户点击按钮后以模态呈现。我想拥有一个“永久”视图的摄像头,就像iOS中的Camera App一样。编程指南总是谈论以模态方式呈现相机,但Instagram等其他应用程序的相机永远是视图的一部分。

我能完成这个吗?怎么样?

谢谢!

1 个答案:

答案 0 :(得分:3)

是的,您可以使用AVFoundation。 导入这些标题:

#import <CoreMedia/CoreMedia.h>
#import <AVFoundation/AVFoundation.h>
#import <QuartzCore/QuartzCore.h>

并使用它来创建AVCaptureVideoPreviewLayer并将其显示在您的视图上。

// Get annd start session
    AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
    [captureSession startRunning];

    // Get preview layer
    AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:captureSession];
    [previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    CGRect layerRect = CGRectMake(0, 0, 320, 460);
    [previewLayer setFrame:layerRect];

    // Get video device
    AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    if (videoDevice) {
        NSError *error;
        AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];

        if (!error) {
            if ([captureSession canAddInput:videoIn]){
                [captureSession addInput:videoIn];
            }
        }
    }

    // Add layer to view
    [[[self view] layer] addSublayer:previewLayer];