ios - MTKView清晰显示

标签 ios opengl-es-2.0 metal metalkit mtkview

我想在应用程序退出事件状态后立即将 MTKView(或 GLKView/CAEAGLLayer)的内容设置为黑色。将其设置为清晰的颜色(比如黑色)并显示它的最快、最可靠的方法是什么?

最佳答案

为了在进入后台时让 MTKView 空白,您必须在从委托(delegate)回调返回到您的 applicationDidEnterBackground(_:) 方法之前渲染一个空白帧UIApplicationDelegate 对象。

监听 UIApplication.didEnterBackgroundNotification 是不够的;在通知观察者收到状态更改通知之前捕获快照。

这意味着您应该将您的应用程序已进入后台的消息从您的应用程序委托(delegate)传递给相关的 View Controller ,并强制它们立即呈现空白帧,之前从委托(delegate)方法返回(意味着没有发布通知,也没有分派(dispatch)异步到不同的线程)。下面是一个将 MTKView 清除为黑色并等待绘制和演示在返回之前安排好的方法:

func drawBlankAndWait(_ mtkView: MTKView) {
    mtkView.clearColor = MTLClearColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
    let commandBuffer = commandQueue.makeCommandBuffer()!
    guard let renderPassDescriptor = mtkView.currentRenderPassDescriptor else { return }
    let renderEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: renderPassDescriptor)!
    renderEncoder.endEncoding()
    let drawable = mtkView.currentDrawable!
    commandBuffer.present(drawable)
    commandBuffer.commit()
    commandBuffer.waitUntilScheduled()
}

收到 applicationWillEnterForeground(_:) 调用后,您可以恢复进入后台时可能设置的任何状态,包括 View 的暂停状态。

关于ios - MTKView清晰显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57557966/

相关文章:

ios - 奇怪的大标题导航 Controller 问题

ios - 在 iOS 上, "add to homepage"缓存保存在哪里,如何清除它?

ios - Xcode 中 IBAction 的函数?

iphone - iPhone OpenGL ES 2.0 中 glReadPixels 的更快替代品

ios - OpenGL photoshop 叠加混合模式

ios - 在 iOS 7 上运行 32 位 arm,在 iOS 8 上运行 64 位

ios - 集成 Google Analytics 后 Aviary SDK 崩溃

android - 使用 SurfaceTexture 和 OpenGL 修改相机输出

buffer - 为什么在 Metal 中不允许从片段着色器中写入缓冲区?

ios - Metal 内核函数中的 3d 纹理插值不适用于 Radeon gpu