how to save rendering image with its original size?

language: swift

i use the metal shaders to render an image which size is (4096, 2304). the mtkView.frame.size is (414.0, 233.0). i convert the drawable.texture to uiimage, and save it. but its size is (828, 466).

how can i save image that its size is (4096, 2304) ?

thanks for your help!

Replies

Your mtkView size is 414x233 points, which is 828x466 pixels with 2x display scaling. By default, the drawable texture size in MTKView follows the view size, which makes sense to render content at the native resolution and perfectly fitting the UIView bounds.

So you have two options:

  • force a specific drawable size on MTKView (disable MTKView.autoResizeDrawable and set MTKView.drawableSize to whatever you want). Texture will have the correct size but it may look weird on display.
  • render to a MTLTexture that you create and own (thus with the size that you want) instead of using the one from the drawable