2016-05-23 67 views
1

在我的應用程序中,我需要閱讀QA代碼。它首次顯示QR碼中的字符串。但是當我第二次嘗試使用它時。它會陷入捕獲的照片模式,但後臺代碼流程正在運行。在我的IOS應用程序中以快速捕捉QR「第二次」時屏幕卡住了

import UIKit 
import AVFoundation 

class BarCodeViewController: UIViewController,AVCaptureMetadataOutputObjectsDelegate { 

var captureSession: AVCaptureSession! 
var previewLayer: AVCaptureVideoPreviewLayer! 
let appDelegat : AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 

override func viewDidLoad() { 
    super.viewDidLoad() 

    view.backgroundColor = UIColor.blackColor() 
    captureSession = AVCaptureSession() 

    let videoCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) 
    let videoInput: AVCaptureDeviceInput 

    do { 
     videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice) 
    } catch { 
     return 
    } 

    if (captureSession.canAddInput(videoInput)) { 
     captureSession.addInput(videoInput) 
    } else { 
     failed(); 
     return; 
    } 

    let metadataOutput = AVCaptureMetadataOutput() 

    if (captureSession.canAddOutput(metadataOutput)) { 
     captureSession.addOutput(metadataOutput) 

     metadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue()) 
     metadataOutput.metadataObjectTypes = [AVMetadataObjectTypeQRCode] 
    } else { 
     failed() 
     return 
    } 

    previewLayer = AVCaptureVideoPreviewLayer(session: captureSession); 
    previewLayer.frame = view.layer.bounds; 
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
    view.layer.addSublayer(previewLayer); 

    captureSession.startRunning(); 

     } 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


func failed() { 
    let ac = UIAlertController(title: "Scanning not supported", message: "Your device does not support scanning a code from an item. Please use a device with a camera.", preferredStyle: .Alert) 
    ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) 
    presentViewController(ac, animated: true, completion: nil) 
    captureSession = nil 
} 

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 

    if (captureSession?.running == false) { 
     captureSession.startRunning(); 
    } 
} 

override func viewWillDisappear(animated: Bool) { 
    super.viewWillDisappear(animated) 

    if (captureSession?.running == true) { 
     captureSession.stopRunning(); 
    } 
} 

func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) { 
    captureSession.stopRunning() 

    if let metadataObject = metadataObjects.first { 
     let readableObject = metadataObject as! AVMetadataMachineReadableCodeObject; 

     AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate)) 
     foundCode(readableObject.stringValue); 
    } 


} 

func foundCode(code: String) { 
    var x = code.componentsSeparatedByString("-") 

    let productProfile = ProductDetailViewController(nibName: "ProductDetailViewController", bundle: nil) 


    productProfile.ProductCode = x[1] 
     dismissViewControllerAnimated(true, completion: nil) 


    appDelegat.centerContainer!.centerViewController = productProfile 
} 

override func prefersStatusBarHidden() -> Bool { 
    return true 
} 

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { 
    return .Portrait 
    } 
} 

我試過,但無法弄清楚..請幫我在這

回答

0
func foundCode(code: String) { 
    var x = code.componentsSeparatedByString("-") 
    let productProfile = ProductDetailViewController(nibName:     "ProductDetailViewController", bundle: nil) 
    productProfile.ProductCode = x[1] 
    captureSession.stopRunning() 
     dismissViewControllerAnimated(true, completion: nil) 
    self.navigationController?.popViewControllerAnimated(false) 
    appDelegat.centerContainer!.centerViewController = productProfile 
} 

添加self.navigationController?.popViewControllerAnimated(false)