When I capture the contents of an MTKView into a UIImage, the resulting image looks qualitatively different, as shown below:
The code I use to generate the UIIm
Your CGColorSpace is .sRGB but your renderPipelineDescriptor's pixelFormat is .bgra8Unorm. Try changing that line to:
CGColorSpace
.sRGB
.bgra8Unorm
renderPipelineDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormat.bgra8Unorm_srgb