iOS正确停止AVCaptureSession

编程入门 行业动态 更新时间:2024-10-20 13:49:44
本文介绍了iOS正确停止AVCaptureSession的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在iOS 7中测试新的条形码扫描api时遇到了问题。此示例(单视图应用程序)工作正常,但我想停止AVCaptureSession并在相机识别出EAN代码后显示第一个视图。

i got a problem while testing the new barcode scanning api in iOS 7. This example (single view application) works fine, but i want to stop the AVCaptureSession and show the first view after an EAN code is recognized by the camera.

[self.captureSession startRunning]; 不起作用。

如何正确停止AVCaptureSession?

How do i correctly stop the AVCaptureSession?

#import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewController () <AVCaptureMetadataOutputObjectsDelegate> @property (strong) AVCaptureSession *captureSession; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.captureSession = [[AVCaptureSession alloc] init]; AVCaptureDevice *videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; NSError *error = nil; AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoCaptureDevice error:&error]; if(videoInput) [self.captureSession addInput:videoInput]; else NSLog(@"Error: %@", error); AVCaptureMetadataOutput *metadataOutput = [[AVCaptureMetadataOutput alloc] init]; [self.captureSession addOutput:metadataOutput]; [metadataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; [metadataOutput setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code]]; AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession]; previewLayer.frame = self.view.layer.bounds; [self.view.layer addSublayer:previewLayer]; [self.captureSession startRunning]; } - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection { for(AVMetadataObject *metadataObject in metadataObjects) { AVMetadataMachineReadableCodeObject *readableObject = (AVMetadataMachineReadableCodeObject *)metadataObject; if([metadataObject.type isEqualToString:AVMetadataObjectTypeQRCode]) { NSLog(@"QR Code = %@", readableObject.stringValue); } else if ([metadataObject.type isEqualToString:AVMetadataObjectTypeEAN13Code]) { NSLog(@"EAN 13 = %@", readableObject.stringValue); } } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }

推荐答案

你实际上可以停止AVCaptureSession因此:

You can actually stop the AVCaptureSession thus:

[self.captureSession stopRunning];

但我怀疑你真正想做的是冻结屏幕。在属性中保留对previewLayer的引用会很有帮助。然后:

But I suspect what you really want to do is to freeze the screen. It's helpful to keep a reference to your previewLayer in a property. Then:

[[self.previewLayer connection] setEnabled:NO];

您可以尝试这样的方法来冻结屏幕,然后在几秒钟后解冻它

You can try something like this to freeze the screen and then unfreeze it after a couple of seconds

- (void) captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection { [[self.previewLayer connection] setEnabled:NO]; double delayInSeconds = 2.0; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ [[self.previewLayer connection] setEnabled:YES]; }); ... }

update 完全删除:

update full takedown:

[self.captureSession stopRunning]; [self.previewLayer removeFromSuperlayer]; self.previewLayer = nil; self.captureSession = nil;

更多推荐

iOS正确停止AVCaptureSession

本文发布于:2023-11-26 00:25:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631927.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确   iOS   AVCaptureSession

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!