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

标签 ios objective-c nslog

我正在编写一个程序来读取二维码,然后使用收集到的数据。我有一个奇怪的问题。我相信它可能必须对线程做一些事情。基本上 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/

相关文章:

ios - 自定义通知式弹出窗口

objective-c - 如果cell == nil,可以安全删除uitableview吗?

ios - 我怎么知道 UITableView 何时完成重新加载

ios - 如何在 AVPlayer 中提取 MPMediaItemCollection 到 NSData?

iphone - iOS 我可以增加一个 NSNumber,还是需要创建一个新的?

ios - UITableview 滚动改变图像,UITableview 滚动问题

ios - 使用 NSAttributedString 进行 AirPrint

iOS - 保存到日志文件并稍后提取

ios - 其他库中的 CocoaLumberjack 和 NSLog

ios - 奇怪的 NSLog 错误。内存泄漏