似乎有很多相似的問題,我所經歷:的Xcode 9 /雨燕4 AVCaptureMetadataOutput setMetadataObjectTypes使用availableMetadataObjectTypes
AVmetadata changes with swift 4 xcode 9
AVCaptureMetadataOutput setMetadataObjectTypes unsupported type found
而且還有一個蘋果的錯誤與交易AVFoundation:
https://forums.developer.apple.com/thread/86810#259270
但這些似乎都不是我的答案。
我有在swift 3中運行得很好的代碼,但只會在swift 4中出錯。使用上述鏈接中的解決方案根本沒有任何變化。
代碼:
import UIKit
import AVFoundation
class BarCodeScanViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
weak var delegate: FlowControllerDelegate?
var captureSession: AVCaptureSession = AVCaptureSession()
var previewLayer: AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.black
captureSession = AVCaptureSession()
guard let videoCaptureDevice = AVCaptureDevice.default(for: .video) else { return }
let videoInput: AVCaptureDeviceInput
do {
videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
} catch {
return
}
if (captureSession.canAddInput(videoInput)) {
captureSession.canAddInput(videoInput)
} else {
failed()
return
}
// let captureMetadataOutput = AVCaptureMetadataOutput()
let metadataOutput = AVCaptureMetadataOutput()
if captureSession.canAddOutput(metadataOutput) {
captureSession.addOutput(metadataOutput)
// Check status of camera permissions
metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
// metadataOutput.metadataObjectTypes = [AVMetadataObject.ObjectType.upce]
metadataOutput.metadataObjectTypes = [.ean8, .ean13, .pdf417, .upce]
} else {
failed()
return
}
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.frame = view.layer.bounds
previewLayer.videoGravity = .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) {
if captureSession.isRunning == true {
captureSession.stopRunning()
}
super.viewWillDisappear(animated)
}
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
captureSession.stopRunning()
if let metatdataObject = metadataObjects.first {
guard let readableObject = metatdataObject as? AVMetadataMachineReadableCodeObject else { return }
guard let stringValue = readableObject.stringValue else { return }
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
found(code: stringValue)
}
dismiss(animated: true)
}
func found(code: String) {
print(code)
}
override var prefersStatusBarHidden: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
}
當我在Xcode中創建8這個代碼,並迅速3,它工作正常。當我在Xcode中運行它,在添加媒體類型9個迅速4IT崩潰:
metadataOutput.metadataObjectTypes = [.ean8, .ean13, .pdf417, .upce]
在這兩種情況下,我建立到沒有公測就可以了以前在iOS設備11。
我試過了「__」,看看它是否是上面提到的Apple bug。 如果我將代碼註釋掉,但代碼沒有捕獲。
蘋果介紹的還有其他一些bug嗎?還有誰有相同的問題嗎?
任何幫助,將不勝感激。
感謝
看起來這可能是Swift 4和Xcode 9的API問題。我們也看到了Xcode <9和9之間的其他類似問題。 – addzo
請參閱:https://stackoverflow.com/a/46027337/1982031 –