(iPhoneX)运行相机时屏幕顶部和底部有空白区域

时间:2017-11-30 04:45:21

标签: ios swift camera qr-code iphone-x

我已经制作了一款在我的iPhone 6 Plus上运行良好的应用程序。该应用程序包含QRCode扫描功能,显然启动相机。运行相机时,它可以全屏显示我的iPhone 6 Plus而不是我的iPhone X.相机仍可正常工作,但相机视图上方和下方都有白条。有什么提示吗?

这里是扫描仪的代码:

import UIKit
import AVFoundation
import SafariServices

class Attend: UIViewController, AVCaptureMetadataOutputObjectsDelegate {

@IBOutlet weak var border: UIImageView!

var video = AVCaptureVideoPreviewLayer()

override func viewDidLoad() {
    super.viewDidLoad()

    let session = AVCaptureSession()

    let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

    do
    {
        let input = try AVCaptureDeviceInput(device: captureDevice)
        session.addInput(input)
    }

    catch
    {
        print ("ERROR")
    }

    let output = AVCaptureMetadataOutput()
    session.addOutput(output)

    output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)

    output.metadataObjectTypes = [AVMetadataObjectTypeQRCode]

    video = AVCaptureVideoPreviewLayer(session: session)
    video.frame = view.layer.bounds
    view.layer.addSublayer(video)

    self.view.bringSubview(toFront: border)

    session.startRunning()


}


func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
    if metadataObjects != nil && metadataObjects.count != 0
    {
        if let object = metadataObjects[0] as? AVMetadataMachineReadableCodeObject
        {
            if object.type == AVMetadataObjectTypeQRCode
            {
                let alert = UIAlertController(title: "QR Code", message: object.stringValue, preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "Retake", style: .default, handler: nil))
                alert.addAction(UIAlertAction(title: "Open", style: .default, handler: { (nil) in
                    UIPasteboard.general.string = object.stringValue
                    self.loadSafari(url: object.stringValue)
                }))
                present(alert, animated: true, completion: nil)

            }
        }
    }

}


func loadSafari(url : String){
    guard let url = URL(string: url) else { return }

    let safariController = SFSafariViewController(url: url)
    present(safariController, animated: true, completion: nil)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}




}

1 个答案:

答案 0 :(得分:0)

1 - 使用LaunchSceen.storyboard而不是启动图像 - 您的底部切割问题将得到解决 2 - 从视图的安全区域制定最高限制