UIlabel 未在 AVCaptureSession Delegate 内更新

发布于 2024-11-08 15:57:27 字数 2503 浏览 0 评论 0原文

我正在学习 Objective 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 但它并不总是打印。标签文本的更改有很多延迟。

有人可以帮忙吗? 谢谢。

I am learning objective c and doing a sample app to fetch video feed from iPhone camera. I was able to get the feeds from camera and display it on screen. Also I was trying to update some UILabel in screen for each frame from the video inside the delegate method. But the label value is not getting updated always. Here is the code I am using

This section will initialize the capture

   - (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]; 


     }

This method is called for each video frame.

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

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

I am trying to print UILabel inside this method but it is not printed always. THere is much delay for the label text to change.

Could someone help please?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸里的那抹悲凉 2024-11-15 15:57:27

您的sampleBufferDelegate的captureOutput是从非主线程调用的 - 从那里更新GUI对象没有任何好处。尝试改用performSelectorOnMainThread。

Your sampleBufferDelegate's captureOutput is being called from a non-main thread - updating GUI objects from there can do no good. Try using performSelectorOnMainThread instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文