2014-10-04 67 views
0

我有這個代碼捕獲條形碼;它可以正常工作,除了在iPad上以外,如果你不能保持iPad穩定,它會「在奶牛回家時嘗試」,如果你有漂移的話。我想添加一個「取消」按鈕或找出一些方法來取消該方法,所以我不必殺死該應用程序並重新啓動它。這是我的代碼:如何添加'取消'按鈕到相機採集代碼?

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection { 

CGRect highlightViewRect = CGRectZero; 
AVMetadataMachineReadableCodeObject *barCodeObject; 
NSString *detectionString = nil; 
NSArray *barCodeTypes = @[AVMetadataObjectTypeEAN13Code]; 

for (AVMetadataObject *metadata in metadataObjects) { 
    for (NSString *type in barCodeTypes) { 
     if ([metadata.type isEqualToString:type]) 
     { 
      barCodeObject = (AVMetadataMachineReadableCodeObject *)[_prevLayer transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject *)metadata]; 
      highlightViewRect = barCodeObject.bounds; 
      detectionString = [(AVMetadataMachineReadableCodeObject *)metadata stringValue]; 
      break; 
     } 
    } 

    if (detectionString != nil) { 
     _label.text = detectionString; 
     oISBNField.text = detectionString; // move detectionString to ISBN textbox 
     [_session stopRunning]; 
     [_prevLayer removeFromSuperlayer]; 
     [_label removeFromSuperview]; 

     break; 
    } 
    else 
     _label.text = @"(none)"; 
} 

} 

有人可以請給我一些幫助嗎?我真的,真的很感激它! :D

回答

1

只需創建一個取消按鈕並將其添加到UIViewController的視圖。

當按下按鈕時,停止捕獲會話並關閉所呈現的視圖控制器。

- (void)cancelButtonPressed:(id)sender { 
    [self.captureSession stopRunning]; //stop the capture session 
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil]; // dismiss the current view controller 
} 
+0

我花了一段時間才知道原來的代碼在做什麼(由於沒有評論),但它現在像一個冠軍!非常感謝! – SpokaneDude 2014-10-07 23:25:49

相關問題