我正在制作一个QR码扫描仪应用程序,我的相机在其中找到该编码并必须隔离到另一个视图中。但是我可以选择另一种视图,因为如果在找到代码后调用self.performSegue,该应用程序将随机关闭。
如果我不打电话给segue,则打印代码,但相机冻结。如果我摆脱了警戒线,应用程序将无法关闭
import UIKit
import Firebase
import AVFoundation
class ScannerViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
var intendedDest = String()
var codex = String()
var captureSession: AVCaptureSession!
var previewLayer: AVCaptureVideoPreviewLayer!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
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.addInput(videoInput)
} else {
failed()
return
}
let metadataOutput = AVCaptureMetadataOutput()
if (captureSession.canAddOutput(metadataOutput)) {
captureSession.addOutput(metadataOutput)
metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.global(qos: .userInteractive))
metadataOutput.metadataObjectTypes = [.ean8, .ean13, .pdf417, .qr]
} 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) {
super.viewWillDisappear(animated)
if (captureSession?.isRunning == true) {
captureSession.stopRunning()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection){
captureSession.stopRunning()
if let metadataObject = metadataObjects.first {
guard let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject else { return }
guard let stringValue = readableObject.stringValue else { return }
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
found(code: stringValue)
}
self.performSegue(withIdentifier: "toTimerView", sender: self)
}
func found(code: String) {
// qr code found!
print(code)
codex = code
print(codex)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? TimerViewController {
destination.intendedDestxx = intendedDest
}
}
override var prefersStatusBarHidden: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
}
'''
答案 0 :(得分:0)
根据警告和崩溃消息,您将在后台线程中访问UI。
确保在主线程中调用了您的UI代码。
如果有后台线程,则可以通过以下方法修复它:
public class Test {
public static Scanner scan;
public static void main(String args[]) {
//To check the length of args --> I take "check" array as an input for my method "eachCycleFCFS".
for (int a = 0; a < args.length; a++) {
check.add(args[a]);
}
try {
String fileAddress = args[0];
File fileInput = new File(fileAddress); //Read
scan = new Scanner(fileInput);
int numProcesses = scan.nextInt();
...
for (int m = 0; m < numProcesses; m++) {
int a = scan.nextInt();
int b = scan.nextInt();
int c = scan.nextInt();
int io = scan.nextInt();
...
}
}
catch (Exception e){
e.printStackTrace();
System.out.printf(" Error: File not foundd. \n");
}
}
public static void eachCycleFCFS (Queue<Process> processes, int numProcesses, Process[] allProcesses, Process[] original, Process[] realOriginal, ArrayList<String> check) {
File fileInput = new File("random-numbers.txt");
Scanner randomInput = null;
try {
randomInput = new Scanner(fileInput);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (check.size() == 2) {
if (check.get(0).contains("verbose")) {
//Produce the detailed output
}
}
else {
//Produce the simple output
}
while (terminatedProcesses != numProcesses) {
if (check.size() == 2) {
if (check.get(0).equals("--verbose")) {
//Produce the detailed output
}
}
}
}
在您的代码中,我看到您将自己设置为后台队列上图像流的委托。这意味着您的segue也被调用为背景。通过以下方法修复它:
blahblahblah$ java Scheduling2 --verbose input-1.txt
java.io.FileNotFoundException: --verbose (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.util.Scanner.<init>(Scanner.java:611)
at Scheduling2.main(Scheduling2.java:18)
Error: File not found.