1

我正在学习目标 c 并做一个示例应用程序来从 iPhone 摄像头获取视频源。我能够从相机获取信息并将其显示在屏幕上。此外,我试图从委托方法内的视频中为每一帧更新屏幕中的一些 UILabel。但是标签值并不总是更新。这是我正在使用的代码

本节将初始化捕获

   - (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 但它并不总是打印出来。标签文本的更改有很多延迟。

有人可以帮忙吗?谢谢。

4

1 回答 1

3

您的 sampleBufferDelegate 的 captureOutput 正在从非主线程调用 - 从那里更新 GUI 对象没有用。尝试改用 performSelectorOnMainThread。

于 2011-05-22T20:30:31.287 回答