我有一个 iOS 应用程序,它使用相机扫描条形码,就这一点而言,一切都很好。但我想在屏幕上放置虚线矩形,以便用户“瞄准”相机。 当我在模拟器中运行应用程序时,矩形显示得很好,但是当我在设备上运行时,我看不到矩形。 我已尝试 BringSubviewToFront 方法,但似乎无法在设备上运行。
以下代码位于ViewDidLoad中:
// Draw a rectangle for aiming the camera
let renderer = UIGraphicsImageRenderer(size:CGSize(width:500, height: 200))
let img = renderer.image {ctx in
ctx.cgContext.setFillColor(UIColor.clear.cgColor)
ctx.cgContext.setStrokeColor(UIColor.red.cgColor)
ctx.cgContext.setLineWidth(7)
// Make it a dashed line - comment out for a solid line
ctx.cgContext.setLineDash(phase:0, lengths: [20])
let rectangle = CGRect(x: 0, y: 0, width: 500, height: 200)
ctx.cgContext.addRect(rectangle)
ctx.cgContext.drawPath(using: .fillStroke)
}
// Create an imageView to hold the rectangle image
let myImageView = UIImageView(image: img)
myImageView.frame = CGRect(x: 147, y: 298, width: 500, height: 250)
myImageView.layer.cornerRadius = 7
cameraView.addSubview(myImageView)
// Add constraints using an extension
myImageView.anchor(top: cameraView.topAnchor,
leading: cameraView.leadingAnchor,
bottom: cameraView.bottomAnchor,
trailing: cameraView.trailingAnchor,
padding: .init(top: 250, left: 100, bottom: 250, right: 100),
size: .init(width: 500, height: 250))
// Bring the rectangle to the front
cameraView.bringSubviewToFront(myImageView)
最佳答案
如果你想为 captureSession 添加图像,你需要创建一个
AVCaptureVideoPreviewLayer
否则如果您需要调用相机服务来拍照拍照,则无法执行此操作。
首先创建变量和 View
var captureSession: AVCaptureSession!
var previewLayer: AVCaptureVideoPreviewLayer!
let overlay: UIImageView = {
let overlay = UIImageView(frame: CGRect.zero)
overlay.image = UIImage(named: "makedCamera")
overlay.contentMode = UIView.ContentMode.scaleToFill
overlay.translatesAutoresizingMaskIntoConstraints = false
return overlay
}()
完成所有捕获 session 代码后
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.frame = view.layer.bounds
previewLayer.videoGravity = .resizeAspectFill
view.layer.addSublayer(previewLayer)
view.addSubview(overlay)
NSLayoutConstraint.activate([
overlay.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
overlay.trailingAnchor.constraint(equalTo: view.trailingAnchor),
overlay.bottomAnchor.constraint(equalTo: view.bottomAnchor),
overlay.leadingAnchor.constraint(equalTo: view.leadingAnchor)
])
captureSession.startRunning()
简单地叠加层就是图像
view.layer.addSublayer(previewLayer)
view.addSubview(overlay)
完整代码:
{
super.viewDidLoad()
view.backgroundColor = UIColor.white
self.title = ""
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.main)
metadataOutput.metadataObjectTypes = [.qr]
} else {
failed()
return
}
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.frame = view.layer.bounds
previewLayer.videoGravity = .resizeAspectFill
view.layer.addSublayer(previewLayer)
view.addSubview(overlay)
NSLayoutConstraint.activate([
overlay.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
overlay.trailingAnchor.constraint(equalTo: view.trailingAnchor),
overlay.bottomAnchor.constraint(equalTo: view.bottomAnchor),
overlay.leadingAnchor.constraint(equalTo: view.leadingAnchor)
])
captureSession.startRunning()
}
关于ios - 显示相机 View 的 ImageView 'On-Top',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57059526/