我想升級mi app到swift 4,但條形碼閱讀器不工作。條碼上swift 4
我已經隔離了條碼閱讀器代碼,但仍然無法工作。相機可以工作,但不會檢測條形碼。
代碼工作就好上迅速3 iOS的10
這是完整的代碼
import AVFoundation
import UIKit
class ViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
var captureSession: AVCaptureSession!
var previewLayer: AVCaptureVideoPreviewLayer!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.black
captureSession = AVCaptureSession()
let videoCaptureDevice = AVCaptureDevice.default(for: AVMediaType.video)
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: DispatchQueue.main)
metadataOutput.metadataObjectTypes = [AVMetadataObject.ObjectType.ean8, AVMetadataObject.ObjectType.ean13, AVMetadataObject.ObjectType.pdf417]
} else {
failed()
return
}
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession);
previewLayer.frame = view.layer.bounds;
previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill;
view.layer.addSublayer(previewLayer);
captureSession.startRunning();
}
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))
present(ac, animated: true)
captureSession = nil
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if (captureSession?.isRunning == false) {
captureSession.startRunning();
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if (captureSession?.isRunning == true) {
captureSession.stopRunning();
}
}
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
captureSession.stopRunning()
if let metadataObject = metadataObjects.first {
let readableObject = metadataObject as! AVMetadataMachineReadableCodeObject;
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
found(code: readableObject.stringValue!);
}
dismiss(animated: true)
}
func found(code: String) {
print(code)
}
override var prefersStatusBarHidden: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
}
我在我的iPhone使用的是iOS 11,升級到公測9
有什麼想法?謝謝。
所以很高興知道這個問題不僅發生在我更新到iOS 11和Swift 4後發生在我的項目中。我使用AVCaptureMetadataOutput對象和AVCaptureMetadataOutputObjectsDelegate委託,在我的應用中有一個非常基本的QR碼閱讀器。我已經證實,一切都在不斷地持續運行,不會中斷。我認爲現在是時候向Apple提交一個bug了(兩者都應該)。唯一改變的是Swift 4中的屬性/函數的名稱,但沒有別的。奇怪我們沒有得到任何委託回調。 –
另外,看着你的代碼,你需要爲你的AVCaptureMetadataOutputObjectsDelegate回調創建一個串行隊列。 metadataOutput.setMetadataObjectsDelegate(self,queue:DispatchQueue.main)。不要使用主隊列,而是在您的視圖控制器中創建一個串行隊列作爲屬性,並在此處使用它而不是主隊列。 –