我有一个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
否则,如果需要致电相机服务进行拍照,则无法这样做。
首先创建变量和视图
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
}()
完成所有捕获会话代码后
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“在顶部”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57059526/