屏幕在捕获QR时卡住;“第二次”;在我的IOS应用程序中
在我的应用程序中,我需要阅读QA代码。这是它第一次在二维码中显示字符串。但当我第二次尝试使用它时。它被困在捕获照片模式中,但支持代码流正在运行屏幕在捕获QR时卡住;“第二次”;在我的IOS应用程序中,ios,swift,Ios,Swift,在我的应用程序中,我需要阅读QA代码。这是它第一次在二维码中显示字符串。但当我第二次尝试使用它时。它被困在捕获照片模式中,但支持代码流正在运行 import UIKit import AVFoundation class BarCodeViewController: UIViewController,AVCaptureMetadataOutputObjectsDelegate { var captureSession: AVCaptureSession! var previewLayer:
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
}
}
我试过了,但想不出来。。请帮我做这件事
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)
添加了self.navigationController?.popViewControllerAnimated(false)