2011-05-22 40 views
1

我正在學習目標c和做一個示例應用程序從iPhone攝像頭獲取視頻源。我能夠從相機獲取供稿並將其顯示在屏幕上。此外,我試圖更新委託方法內的視頻中的每個幀的一些UILabel。但標籤值始終沒有得到更新。下面是我使用UIlabel沒有得到更新AVCaptureSession裏面委託

本節將初始化捕獲

- (void)initCapture 
{ 
    NSError *error = nil; 
    device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 

    if ([device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus] && [device lockForConfiguration:&error]) { 
     [device setFocusMode:AVCaptureFocusModeContinuousAutoFocus]; 
     [device unlockForConfiguration]; 
    } 

    AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil]; 

    //AVCaptureStillImageOutput *imageCaptureOutput = [[AVCaptureStillImageOutput alloc] init]; 

    AVCaptureVideoDataOutput *captureOutput =[[AVCaptureVideoDataOutput alloc] init]; 

    captureOutput.alwaysDiscardsLateVideoFrames = YES; 
    //captureOutput.minFrameDuration = CMTimeMake(1, 1); 

    captureOutput.alwaysDiscardsLateVideoFrames = YES; 
    dispatch_queue_t queue; 
    queue = dispatch_queue_create("cameraQueue", NULL); 
    [captureOutput setSampleBufferDelegate:self queue:queue]; 
    dispatch_release(queue); 
    // Set the video output to store frame in BGRA (It is supposed to be faster) 
    NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey; 
    NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA]; 
    NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key]; 
    [captureOutput setVideoSettings:videoSettings]; 

    self.captureSession = [[AVCaptureSession alloc] init]; 

    [self.captureSession addInput:captureInput]; 
    [self.captureSession addOutput:captureOutput]; 


    self.prevLayer = [AVCaptureVideoPreviewLayer layerWithSession: self.captureSession]; 
    self.prevLayer.frame = CGRectMake(0, 0, 320, 320); 
    self.prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 



    [self.videoPreview.layer addSublayer: self.prevLayer];  

    [self.captureSession startRunning]; 


    } 

這種方法被稱爲爲每個視頻幀的代碼。

#pragma mark AVCaptureSession delegate 
- (void)captureOutput:(AVCaptureOutput *)captureOutput 
    didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
    fromConnection:(AVCaptureConnection *)connection 
    { 

     i++; 
     self.lblStatus.Text = [NSString stringWithFormat:@"%d",i]; 
    } 

我想在此方法內打印UILabel,但它並不總是打印。標籤文本發生變化的時間很長。

有人可以幫忙嗎? 謝謝。

+0

你怎麼知道它不是一直都在打印。你錯過了幾個'我'的價值嗎? – 2011-05-22 17:27:09

+0

沒有它的不是.....甚至i = 1在與其他值相同的長時間後打印。 – Zach 2011-05-22 17:34:13

+0

哇,我有幾乎完全相同的問題,但認爲它太晦澀,找不到其他人遇到相同的問題。我知道它與它所在的線程有關,因爲它只會在某些時候更新。祝你和你的努力,扎克!也許我們正在開展類似的項目。哈哈。 – 2011-08-13 22:14:51

回答

3

你的sampleBufferDelegate的captureOutput被一個非主線程調用 - 從那裏更新GUI對象不會有任何好處。嘗試使用performSelectorOnMainThread來代替。

+0

謝謝.....現在它的工作正常。 – Zach 2011-05-22 21:41:32