UIlabel не обновляется внутри делегата AVCaptureSession - PullRequest
1 голос
/ 22 мая 2011

Я изучаю цель 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 внутри этого метода, но он печатается не всегда. Существует большая задержка для изменения текста метки.

Может кто-нибудь помочь, пожалуйста? Спасибо.

1 Ответ

3 голосов
/ 23 мая 2011

Ваш метод captureOutput для sampleBufferDelegate вызывается из неосновного потока - обновление объектов GUI оттуда не принесет пользы. Попробуйте вместо этого использовать executeSelectorOnMainThread.

...