【问题标题】:SwiftUI Position Overlay on Camera Preview相机预览上的 SwiftUI 位置叠加
【发布时间】:2020-05-19 20:07:47
【问题描述】:

我正在尝试在使用 某些设备上的肖像和其他设备上的风景。我可以手动设置坐标但是 一直无法获取相机预览框的正确坐标。

这是一个 SwiftUI 应用程序,所以我在一个 UIViewController 可表示。这就是我所拥有的,显然,目标圈是 当设备和/或方向改变时总是错误的。我似乎无法捕捉 相机预览所在的模态视图的框架。我愿意成为 能够在模态视图上指定相机预览的框架和位置。

struct CaptureImageView: View {
    @Binding var isShown: Bool
    @Binding var image: Image?
    @Binding var newUIImage: UIImage?
    @Binding var showSaveButton: Bool

    func makeCoordinator() -> Coordinator {
        return Coordinator(isShown: $isShown, image: $newUIImage, showSaveButton: $showSaveButton)
    }
}

extension CaptureImageView: UIViewControllerRepresentable {

    func makeUIViewController(context: UIViewControllerRepresentableContext<CaptureImageView>) -> UIImagePickerController {

        let vc = UIImagePickerController()

        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera) {
            vc.sourceType = .camera
            vc.allowsEditing = true
            vc.delegate = context.coordinator

            let screenSize: CGRect = UIScreen.main.bounds
            let screenWidth = screenSize.width
            let screenHeight = screenSize.height

            vc.cameraOverlayView = CircleView(frame: CGRect(x: (screenWidth / 2) - 50, y: (screenWidth / 2) + 25, width: 100, height: 100))

            return vc
        }
        return UIImagePickerController()
    }

    func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<CaptureImageView>) {
    }
}

这是一个想法 - 但目标始终需要居中:

还有目标文件:

class CircleView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.clear
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func draw(_ rect: CGRect) {
        if let context = UIGraphicsGetCurrentContext() {
            context.setLineWidth(3.0)
            UIColor.red.set()

            let center = CGPoint(x: frame.size.width / 2, y: frame.size.height / 2)
            let radius = (frame.size.width - 10) / 2

            context.addArc(center: center, radius: radius, startAngle: 0.0, endAngle: .pi * 2.0, clockwise: true)
            context.strokePath()
        }
    }
}

任何指导将不胜感激。 Xcode 版本 11.3.1 (11C504)

【问题讨论】:

    标签: ios xcode camera swiftui uiimagepickercontroller


    【解决方案1】:

    虽然我仍然想找到获取相机框架的方法的答案 - 我的目标主要可以通过在 ZStack 中创建一个圆形视图来实现,将相机视图放在我在 SwiftUI 中的圆形视图后面,如下所示:

    if showCaptureImageView {
        ZStack {
            CaptureImageView(isShown: $showCaptureImageView, image: $myImage, newUIImage: $newUIImage, showSaveButton: $showSaveButton)
            
            Circle()
                .stroke(Color.red, style: StrokeStyle(lineWidth: 10 ))
                .frame(width: 100, height: 100)
                .offset(CGSize(width: 0, height: -50.0))
        }
    }//if show
    

    【讨论】:

    【解决方案2】:

    正如 GrandSteph 所说,更好的方法是将相机视图包装在 Geometry Reader 中。这是一种方法:

    if showCaptureImageView {
        ZStack {
            VStack {
                Rectangle()//just to show that the circle below can be centered
                        .frame(width: 300, height: 150)
                        .background(Color.blue)
    
                GeometryReader { geo in
                    CaptureImageView(isShown: self.$showCaptureImageView, image: self.$image)
                    .onAppear {
                        self.cameraWindowWidth = geo.size.width
                        self.cameraWindowHeight = geo.size.height
                    }
                }//geo
            }//v
    
            Circle().frame(width: 50, height: 50)
            .position(x: self.cameraWindowWidth / 2, y: (self.cameraWindowHeight / 2) + 150)
            .foregroundColor(.red)
    
        }//z
    }//if show
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-06
      • 2014-10-11
      • 1970-01-01
      • 2016-11-13
      • 1970-01-01
      • 2011-11-08
      • 2015-04-13
      相关资源
      最近更新 更多