2011-10-19 44 views
3

我正在开发一个应用程序,从其他人处接管开发。他们呈现的相机在用户点击按钮后以模态呈现。我想拥有一个“永久”视图的相机,如iOS中的相机应用程序。编程指南始终谈论如何以模态方式呈现相机,但其他应用程序(例如Instagram)则拥有永久成为视图一部分的相机。我可以有没有模态显示的相机?怎么样?

我可以做到这一点吗?怎么样?

谢谢!

回答

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]; 
+0

太好了!非常感谢,请尽快尝试。但是,这仅用于视频捕获吗?照片怎么样? –

+0

此代码仅用于在视图上显示您的iphone的摄像头。 – yinkou

相关问题