2011-10-19 37 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

相關問題