【发布时间】:2018-02-11 03:59:44
【问题描述】:
我正在尝试将 mi 应用升级到 swift 4,但条形码阅读器无法正常工作。
我已经隔离了条形码阅读器代码,但仍然无法正常工作。摄像头可以工作,但无法检测到条形码。
代码在 swift 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,升级到 beta 9。
有什么想法吗?谢谢。
【问题讨论】:
-
所以很高兴知道这个问题不仅仅发生在我的项目更新到 iOS 11 和 Swift 4 之后。我的应用程序中也有一个非常基本的二维码阅读器,它使用 AVCaptureMetadataOutput 对象和 AVCaptureMetadataOutputObjectsDelegate 委托。我已经验证一切都在持续不断地运行并且没有中断。我认为此时是向 Apple 提交错误的时候了(两者都应该使用)。唯一改变的是 Swift 4 中的属性/函数的名称,没有别的。奇怪的是我们没有收到任何委托回调。
-
另外,查看您的代码,您需要为您的 AVCaptureMetadataOutputObjectsDelegate 回调创建一个串行队列。 metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)。不要使用主队列,而是在视图控制器中创建一个串行队列作为属性并在此处使用它而不是主队列。
-
仅供参考,可用于第三方github.com/mahendragp/MGPBarcodeScanner
标签: barcode avcapturesession ios11 swift4 xcode9-beta