gpt4 book ai didi

ios - 延迟背景颜色和标签更新但 NSLog 工作正常

转载 作者:行者123 更新时间:2023-11-28 19:03:39 25 4
gpt4 key购买 nike

我正在编写一个程序来读取二维码,然后使用收集到的数据。我有一个奇怪的问题。我相信它可能必须对线程做一些事情。基本上 if 语句中的所有 NSLogs 都工作正常并且每秒快速打印但是当我尝试更新标签文本或更改背景颜色时需要大约 40 秒。我不明白为什么。这是代码。

#import "VerifyPassViewController.h"
#import "NewPassViewController.h"

@interface VerifyPassViewController ()



-(BOOL)startReading;
-(void)stopReading;
-(void)loadBeepSound;

@end

@implementation VerifyPassViewController



- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

// Initially make the captureSession object nil.
_captureSession = nil;

// Set the initial value of the flag to NO.
_isReading = NO;


}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


#pragma mark - IBAction method implementation

- (IBAction)startStopReading:(id)sender {
if (!_isReading) {
// This is the case where the app should read a QR code when the start button is tapped.
if ([self startReading]) {
// If the startReading methods returns YES and the capture session is successfully
// running, then change the start button title and the status message.
[_bbITem setTitle:@"Stop"];
[_bbITem setTitle:@"Scanning for QR Code..."];
}
}
else{
// In this case the app is currently reading a QR code and it should stop doing so.
[self stopReading];
// The bar button item's title should change again.
[_bbITem setTitle:@"Start!"];
}

// Set to the flag the exact opposite value of the one that currently has.
_isReading = !_isReading;
}


#pragma mark - Private method implementation

- (BOOL)startReading {
NSError *error;

// Get an instance of the AVCaptureDevice class to initialize a device object and provide the video
// as the media type parameter.
AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

// Get an instance of the AVCaptureDeviceInput class using the previous device object.
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];

if (!input) {
// If any error occurs, simply log the description of it and don't continue any more.
NSLog(@"%@", [error localizedDescription]);
return NO;
}

// Initialize the captureSession object.
_captureSession = [[AVCaptureSession alloc] init];
// Set the input device on the capture session.
[_captureSession addInput:input];


// Initialize a AVCaptureMetadataOutput object and set it as the output device to the capture session.
AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];
[_captureSession addOutput:captureMetadataOutput];

// Create a new serial dispatch queue.
dispatch_queue_t dispatchQueue;
dispatchQueue = dispatch_queue_create("myQueue", NULL);
[captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];
[captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeQRCode]];

// Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
_videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
[_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[_videoPreviewLayer setFrame:_viewPreview.layer.bounds];
[_viewPreview.layer addSublayer:_videoPreviewLayer];


// Start video capture.
[_captureSession startRunning];

return YES;
}


-(void)stopReading{
// Stop video capture and make the capture session object nil.
[_captureSession stopRunning];
_captureSession = nil;

// Remove the video preview layer from the viewPreview view's layer.
[_videoPreviewLayer removeFromSuperlayer];
}




#pragma mark - AVCaptureMetadataOutputObjectsDelegate method implementation
- (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);


self.view.backgroundColor = [UIColor greenColor];

NSLog(@"If the background isnt green i might throw up");





}
else if ([metadataObject.type isEqualToString:AVMetadataObjectTypeEAN13Code])
{
NSLog(@"EAN 13 = %@", readableObject.stringValue);
}
}
}





@end

如果您有任何想法,请告诉我。谢谢

最佳答案

您只能从主线程更新 UI:

dispatch_async(dispatch_get_main_queue(), ^{
... update here your ui ..
});

关于ios - 延迟背景颜色和标签更新但 NSLog 工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22541696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com