An object that decodes common image formats into Metal textures for use in your app.
SDKs
- iOS 9.0+
- macOS 10.11+
- Mac Catalyst 13.0+
- tvOS 9.0+
Framework
- Metal
Kit
Declaration
@interface MTKTextureLoader : NSObject
Overview
Use MTKTexture
to create a Metal texture with preexisting data as its contents. This class can read common file formats like PNG, JPEG, and TIFF. It can also load image data from KTX and PVR files, asset catalogs, Core Graphics images, and other sources. It infers the output texture format and pixel format from the image data. You can specify options to alter the image loading and texture creation process.
You can create textures synchronously or asynchronously. MTKTexture
methods return MTLTexture
objects. You are responsible for maintaining strong references to the textures after you create them.
Initialize a MTKTexture
object using the device object that it should use to create textures, then call one of the texture loader's methods to create a texture. The code listing below synchronously creates a texture from data stored at a URL, using the default options:
- (id<MTLTexture>)loadTextureUsingMetalKit: (NSURL *) url device: (id<MTLDevice>) device {
MTKTextureLoader *loader = [[MTKTextureLoader alloc] initWithDevice: device];
id<MTLTexture> texture = [loader newTextureWithContentsOfURL:url options:nil error:nil];
if(!texture)
{
NSLog(@"Failed to create the texture from %@", url.absoluteString);
return nil;
}
return texture;
}
You'd typically use MTKTexture
to create a texture when you need basic texture loading behavior. If you have custom data formats, or need to change the texture's contents at runtime, use methods on MTLTexture
instead. For more information, see Creating and Sampling Textures.