我在视图中有UIActivityIndicator
位置,我尝试在扫描条形码后显示它(因为某些原因它不会立即转到我的带有加载屏幕的信息页面我试着在CaptureOutput
中显示指标,但[loadView setHidden:NO]
被忽略了。为什么会这样?还有一种方法可以在扫描后立即进行扫描吗?
BRISBNScanner.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface BRISBNScanner : UIViewController <AVCaptureMetadataOutputObjectsDelegate>
{
IBOutlet UIActivityIndicatorView *loadView;
}
@property (strong, nonatomic) AVCaptureDevice* device;
@property (strong, nonatomic) AVCaptureDeviceInput* input;
@property (strong, nonatomic) AVCaptureMetadataOutput* output;
@property (strong, nonatomic) AVCaptureSession* session;
@property (strong, nonatomic) AVCaptureVideoPreviewLayer* preview;
@property (strong, nonatomic) NSString *isbnText;
@end
BRISBNScanner.m
#import "BRISBNScanner.h"
#import "BRScanInfoView.h"
@interface BRISBNScanner ()
@end
@implementation BRISBNScanner
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// Device
self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// Input
self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
// Output
self.output = [[AVCaptureMetadataOutput alloc] init];
[self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
// Session
self.session = [[AVCaptureSession alloc] init];
[self.session addInput:self.input];
[self.session addOutput:self.output];
self.output.metadataObjectTypes = @[AVMetadataObjectTypeEAN13Code];
// Preview
self.preview = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
self.preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.preview.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view.layer insertSublayer:self.preview atIndex:0];
// Start
[self.session startRunning];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
[loadView setHidden:NO];
[self.session stopRunning];
for(AVMetadataObject *metadataObject in metadataObjects)
{
AVMetadataMachineReadableCodeObject *readableObject = (AVMetadataMachineReadableCodeObject *)metadataObject;
if ([metadataObject.type isEqualToString:AVMetadataObjectTypeEAN13Code])
{
NSLog(@"EAN 13 = %@", readableObject.stringValue);
_isbnText = readableObject.stringValue;
[loadView setHidden:YES];
[self performSegueWithIdentifier:@"showInfo" sender:self];
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray: self.navigationController.viewControllers];
[viewControllers removeObjectIdenticalTo:self];
[self.navigationController setViewControllers: viewControllers animated: YES];
}
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showInfo"]) {
BRScanInfoView *scanView = (BRScanInfoView *)[segue destinationViewController];
//The view that uses the ISBN to search for stuff
scanView.isbnTextprop = _isbnText;
}
}
@end
答案 0 :(得分:1)
除非会话的metadataObjectsCallbackQueue设置为主队列(或者设置为在其上执行的队列),否则您不会从主线程调用[loadView setHidden:NO]。 (这可能也是为什么[self performSegueWithIdentifier:@“showInfo”sender:self]不会立即转到您的信息页面)。由于大部分UIKit都不是线程安全的,因此导致“未定义”行为,在这种情况下不会改变任何内容或仅在延迟之后。
我的建议是将所有ui-updates包装在
中dispatch_async(dispatch_get_main_queue(), ^{
//ui-updates
});