You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

iPad端自定义相机全屏显示问题求助

iPad端自定义相机全屏显示问题求助

各位大佬好,我是Swift开发新手,最近在做一个自定义相机的功能,现在卡在了iPad横屏时相机视图无法完全填满屏幕的问题上,上下始终留着黑边,试了一些方法没解决,想请大家帮忙看看🥺

问题背景

我基于一份自定义相机的代码修改,已经实现了只显示相机视图、支持横屏的效果,但MTKView始终没法铺满整个屏幕,上下区域有黑边。我尝试过设置modalPresentationStyle = .overFullScreen,但没有任何变化。

关键尺寸打印信息

我在draw(in view: MTKView)方法里打了几个关键尺寸,想排查适配问题:

  • ciImage.extent.width 输出:2430.0
  • ciImage.extent.height 输出:1830.0
  • view.drawableSize 输出:(1688.0, 780.0)

相关代码片段

1. SceneDelegate.swift

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let windowScene = (scene as? UIWindowScene) else { return }
        window = UIWindow(windowScene: windowScene)
        let rootVC = ViewController()
        rootVC.modalPresentationStyle = .overFullScreen
        window?.rootViewController = rootVC
        window?.makeKeyAndVisible()
    }
}

2. ViewController的视图布局方法

extension ViewController {
    //MARK:- View Setup
    func setupView(){
        view.backgroundColor = .purple
        mtkView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(mtkView)
        view.addSubview(switchCameraButton)
        
        NSLayoutConstraint.activate([
            switchCameraButton.widthAnchor.constraint(equalToConstant: 30),
            switchCameraButton.heightAnchor.constraint(equalToConstant: 30),
            switchCameraButton.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 10),
            switchCameraButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -10),
            
            mtkView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            mtkView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            mtkView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            mtkView.topAnchor.constraint(equalTo: view.topAnchor)
        ])
        
        switchCameraButton.addTarget(self, action: #selector(switchCamera(_:)), for: .touchUpInside)
    }

3. MTKViewDelegate的绘制方法

extension ViewController : MTKViewDelegate {
    func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {
        //tells us the drawable's size has changed
        //NSLog("MTKView drawable size will change to \(size)")
    }
    
    func draw(in view: MTKView) {
        //create command buffer for ciContext to use to encode it's rendering instructions to our GPU
        guard let commandBuffer = metalCommandQueue.makeCommandBuffer() else { return }
        //make sure we actually have a ciImage to work with
        guard var ciImage = currentCIImage else { return }
        //make sure the current drawable object for this metal view is available (it's not in use by the previous draw cycle)
        guard let currentDrawable = view.currentDrawable else { return }
        
        print(ciImage.extent.width) // 这行打印2430.0
        print(ciImage.extent.height) // 这行打印1830.0
        print(view.drawableSize) // 这行打印(1688.0, 780.0)
        
        //make sure frame is centered on screen
        let heightOfciImage = ciImage.extent.height
        let heightOfDrawable = view.drawableSize.height
        let yOffsetFromBottom = (heightOfDrawable - heightOfciImage)/2
        
        //render into the metal texture
        self.ciContext.render(ciImage, to: currentDrawable.texture, commandBuffer: commandBuffer, bounds: CGRect(origin: CGPoint(x: 0, y: -yOffsetFromBottom), size: view.drawableSize), colorSpace: CGColorSpaceCreateDeviceRGB())
        
        //register where to draw the instructions in the command buffer once it executes
        commandBuffer.present(currentDrawable)
        //commit the command to the queue so it executes
        commandBuffer.commit()
    }
}

我的疑问

目前我已经把MTKView的四边约束都贴到了view的边缘,但还是有黑边,是不是相机输出的CIImage尺寸和MTKView的drawableSize比例不匹配导致的?应该怎么调整才能让相机画面完全铺满整个屏幕呢?麻烦各位大佬指点一下,谢谢啦!

火山引擎 最新活动