I am trying to load an asset image in Metal like so:
let textureLoader = MTKTextureLoader(device: context.device)
do{
let image = UIImage(named: name)
try texture = textureLoader.newTextureWithCGImage(image!.CGImage!, options: [:])
}catch let error{
print("Failed to create texture, error \(error)")
}
I am able to display the image but the colors are permuted, as if the file contains RGB data but it is being interpreted as BGR data. I know the default color space for a Metal layer is BGRA8Unorm
but I don't know how to force the image to be loaded in that format.
You should use the designated class method on MTKTextureLoader
to create a new MTLTexture
from an URL:
let device = MTLCreateSystemDefaultDevice()!
let loader = MTKTextureLoader(device: device)
let url = NSBundle.mainBundle().URLForResource("pic", withExtension: "jpg")!
let texture = try! loader.newTextureWithContentsOfURL(url, options: nil)