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 に答える