0
私は降下の方法でカメラを閉じたいと思います。Swift 3 AVfoundation - カメラを閉じることができません
import UIKit
import AVFoundation
var QRcode = "test"
var myAddress = ""
class ScanController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
@IBOutlet weak var TEst: UIButton!
@IBAction func CloseSession(_ sender: Any) {
captureSession.stopRunning()
previewLayer?.removeFromSuperlayer()
}
var desc = String()
@IBOutlet weak var messageLabel: UILabel!
var videoCaptureDevice: AVCaptureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
var device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
var output = AVCaptureMetadataOutput()
var previewLayer: AVCaptureVideoPreviewLayer?
var captureSession = AVCaptureSession()
var code: String?
@IBOutlet weak var Messagelabel: UILabel!
func loadCookies() {
guard let cookieArray = UserDefaults.standard.array(forKey: "savedCookies") as? [[HTTPCookiePropertyKey: Any]] else { return }
for cookieProperties in cookieArray {
if let cookie = HTTPCookie(properties: cookieProperties) {
HTTPCookieStorage.shared.setCookie(cookie)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.clear
self.setupCamera()
view.bringSubview(toFront: Messagelabel)
view.bringSubview(toFront: TEst)
}
private func setupCamera() {
let input = try? AVCaptureDeviceInput(device: videoCaptureDevice)
if self.captureSession.canAddInput(input) {
self.captureSession.addInput(input)
}
self.previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
if let videoPreviewLayer = self.previewLayer {
videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
videoPreviewLayer.frame = view.layer.bounds
view.layer.addSublayer(videoPreviewLayer)
}
let metadataOutput = AVCaptureMetadataOutput()
if self.captureSession.canAddOutput(metadataOutput) {
self.captureSession.addOutput(metadataOutput)
metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
metadataOutput.metadataObjectTypes = [AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code]
} else {
print("Could not add metadata output")
}
}
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!) {
// This is the delegate'smethod that is called when a code is readed
for metadata in metadataObjects {
let readableObject = metadata as! AVMetadataMachineReadableCodeObject
let code = readableObject.stringValue
Messagelabel.text = code
captureSession.stopRunning();
//self.dismiss(animated: true, completion: nil)
}
}
}
CloseSessionボタンアクションは、TEカメラを閉じる必要があります。しかし、私は、コードは下記を参照してください....私はキャプチャセッションとremoveFromSuperlayerを停止してそれを閉じたときに、私は黒い画面を取得し、それが働いて得ることはありませんボタンを押すと黒い画面が表示され、凍っています。