用金属加载纹理的首选方法

时间:2016-08-14 05:28:08

标签: swift image graphics metal

我正在尝试将资产图像加载到金属中,如下所示:

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)")
}

我可以显示图像,但颜色是置换的,就像文件包含RGB数据一样,但它被解释为BGR数据。我知道金属图层的默认颜色空间是BGRA8Unorm,但我不知道如何强制以该格式加载图像。

1 个答案:

答案 0 :(得分:0)

您应该使用MTKTextureLoader上指定的类方法从网址创建新的MTLTexture

let device = MTLCreateSystemDefaultDevice()!
let loader = MTKTextureLoader(device: device)
let url = NSBundle.mainBundle().URLForResource("pic", withExtension: "jpg")!
let texture = try! loader.newTextureWithContentsOfURL(url, options: nil)