[英]iOS Correctly stop AVCaptureSession
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. 我在iOS 7中测试新的条形码扫描api时遇到了问题。这个例子(单视图应用程序)工作正常,但我想停止AVCaptureSession并在相机识别出EAN代码后显示第一个视图。
[self.captureSession startRunning];
does not work. 不起作用。
How do i correctly stop the AVCaptureSession? 我如何正确停止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.
}
You can actually stop the AVCaptureSession thus: 你可以实际上停止AVCaptureSession:
[self.captureSession stopRunning];
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. 在属性中保留对previewLayer的引用很有帮助。 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 更新
full takedown: 完全删除:
[self.captureSession stopRunning];
[self.previewLayer removeFromSuperlayer];
self.previewLayer = nil;
self.captureSession = nil;
you should call the performViewController in the main thread. 你应该在主线程中调用performViewController。 such as below: 如下:
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
if (metadataObjects != nil && [metadataObjects count] > 0) {
AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];
if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {
[self performSelectorOnMainThread:@selector(stopReading) withObject:nil waitUntilDone:NO];
// should call the view controller transfer method in the main thread
dispatch_async(dispatch_get_main_queue(), ^{
[self performSegueWithIdentifier:@"show first view controller" sender: self];
});
}
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.