I'm doing an iOS application that need verification on QR Code with the Hierarchy like this:

View
---Scan View
---Image View - cardBG
---Inside View
  1. When view is loading, the Scan View is hidden.
  2. When user click button to scan, the Inside View and Image View is set hidden, reveal the Scan View.
  3. After the scanning return success, the Inside and Image appear again.

The problem is in step 3: when I stop AVCaptureSession, even in an asynchronous dispatch like in this question, the it takes 8-10 seconds to refresh the view.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    if([_captureSession isRunning])[_captureSession stopRunning];
    AVCaptureInput* input = [_captureSession.inputs objectAtIndex:0];
    [_captureSession removeInput:input];
    AVCaptureVideoDataOutput* output = (AVCaptureVideoDataOutput*)[_captureSession.outputs objectAtIndex:0];
    [_captureSession removeOutput:output];
});

[self.bgImageView setHidden:NO];
[self.insideView setHidden:NO];
[self.scanView setHidden:YES];
[self.previewLayer removeFromSuperlayer];

My question is: How can I get the view to avoid this freeze?

有帮助吗?

解决方案

Hard to say without more context. Depends on what is actually causing the delay. Would something like this work?

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    if([_captureSession isRunning])[_captureSession stopRunning];

    dispatch_async(dispatch_get_main_queue(), ^{
      [self.bgImageView setHidden:NO];
      [self.insideView setHidden:NO];
      [self.scanView setHidden:YES];
      [self.previewLayer removeFromSuperlayer];        
    });

    AVCaptureInput* input = [_captureSession.inputs objectAtIndex:0];
    [_captureSession removeInput:input];
    AVCaptureVideoDataOutput* output = (AVCaptureVideoDataOutput*)[_captureSession.outputs objectAtIndex:0];
    [_captureSession removeOutput:output];    

});

其他提示

The following code will help you:

if(self.previewLayer) {
    [self.previewLayer removeFromSuperlayer];
    self.previewLayer = nil;
}

if(_captureSession) {
    [_captureSession stopRunning];
    _captureSession = nil;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top