Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

71
Vistas
MTKView - Resizing Texture To Fill View

I am very new to Metal, but diligently working to follow Apple's AVCamFilter sample project. The project demonstrates using a MTKView as a preview for an AVCaptureSession.

I have been unsuccessful in figuring out how to have my MTKView render "full screen" (specifically on iPhone X, XS, and 3rd generation iPad Pro). While my constraints are set properly in the Storyboard, my camera preview is scaled to a different aspect ratio, and not full screen.

As a test, I set;

self.clearColor = MTLClearColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0)

in my MTKView's init, confirming that the MTKView is the proper size (I can see the red background in the problematic areas, but my camera preview does not stretch to fill the screen).

I am of the belief that my issue exists in this calculation;

// Calculate scale.
if textureWidth > 0 && textureHeight > 0 {
  switch textureRotation {
    case .rotate0Degrees, .rotate180Degrees:
      scaleX = Float(internalBounds.width / CGFloat(textureWidth))
      scaleY = Float(internalBounds.height / CGFloat(textureHeight))

    case .rotate90Degrees, .rotate270Degrees:
      scaleX = Float(internalBounds.width / CGFloat(textureHeight))
      scaleY = Float(internalBounds.height / CGFloat(textureWidth))
   }
}

// Resize aspect ratio.
resizeAspect = min(scaleX, scaleY)
if scaleX < scaleY {
  scaleY = scaleX / scaleY
  scaleX = 1.0
} else {
  scaleX = scaleY / scaleX
  scaleY = 1.0
}

In my test environment, my texture size is 2400x1800 and my internal bounds are 834x1194. While I recognize the difference in aspect ratio, I am trying to figure out the correct math to have the texture fill the entire display (even if that means it is scaled slightly less and I lose some of the texture on the sides).

Could anyone advise? Thanks!

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

// Resize aspect ratio.
resizeAspect = min(scaleX, scaleY)

if scaleX < scaleY {
  scaleY = scaleX / scaleY
  scaleX = 1.0
} else {
  scaleX = scaleY / scaleX
  scaleY = 1.0
}

Replace with

// Resize aspect ratio.
// For AspectFill Screen scaleX > scaleY
// For AspectFit Screen scaleX < scaleY
    
resizeAspect = min(scaleX, scaleY)
if scaleX > scaleY {
   scaleY = scaleX / scaleY
   scaleX = 1.0
} else {
   scaleX = scaleY / scaleX
  scaleY = 1.0
}
over 4 years ago · Santiago Trujillo Denunciar

0

Open PreviewMetalView.swift

Replace following lines:
let width = CVPixelBufferGetWidth(previewPixelBuffer)
let height = CVPixelBufferGetHeight(previewPixelBuffer)

with following lines: 
let previewViewWidth = Int(self.frame.size.width)
let previewViewheight = Int(self.frame.size.height)
let isPortrait = previewViewheight > previewViewWidth
let width = isPortrait ? previewViewheight : previewViewWidth
let height = isPortrait ? previewViewWidth : previewViewheight
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda