<!--
{
  "availability" : [
    "iOS: 5.0.0 - 12.0.0",
    "iPadOS: 5.0.0 - 12.0.0",
    "macOS: 10.8.0 - 10.14.0",
    "tvOS: 9.0.0 - 12.0.0"
  ],
  "documentType" : "symbol",
  "framework" : "GLKit",
  "identifier" : "/documentation/GLKit/GLKTextureLoader/texture(with:options:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "GLKit"
    ],
    "preciseIdentifier" : "c:objc(cs)GLKTextureLoader(cm)textureWithCGImage:options:error:"
  },
  "title" : "texture(with:options:)"
}
-->

# texture(with:options:)

Loads a 2D texture image from a Quartz image and creates a new texture from the data.

```
class func texture(with cgImage: CGImage, options: [String : NSNumber]? = nil) throws -> GLKTextureInfo
```

## Parameters

`cgImage`

The Quartz image to be turned into a texture.

`options`

A dictionary that describes any additional steps you want the texture loader to take when loading the texture. See [Texture Loading Options](/documentation/GLKit/texture-loading-options).

## Return Value

A texture info object that describes the loaded texture or `nil` if an error occurred.

## Discussion

This class method loads the texture into the sharegroup attached to the current context for the thread this method is called on.

If the image was created using the CGBitmapImageContextCreate function, it must use one of the pixel formats described in the table below. CGImages loaded from files typically are already in one of these formats.

|Color Space|Pixel format and bitmap information constant                                                                 |
|-----------|-------------------------------------------------------------------------------------------------------------|
|Null       |8 bpp, 8 bpc, <doc://com.apple.documentation/documentation/CoreGraphics/CGImageAlphaInfo/alphaOnly>          |
|Gray       |8 bpp, 8 bpc, <doc://com.apple.documentation/documentation/CoreGraphics/CGImageAlphaInfo/none>               |
|Gray       |8 bpp, 8 bpc, <doc://com.apple.documentation/documentation/CoreGraphics/CGImageAlphaInfo/alphaOnly>          |
|RGB        |32 bpp, 8 bpc, <doc://com.apple.documentation/documentation/CoreGraphics/CGImageAlphaInfo/noneSkipFirst>     |
|RGB        |32 bpp, 8 bpc, <doc://com.apple.documentation/documentation/CoreGraphics/CGImageAlphaInfo/premultipliedFirst>|

> Handling Errors in Swift:
> In Swift, this method returns a nonoptional result and is marked with the `throws` keyword to indicate that it throws an error in cases of failure.
> 
> You call this method in a `try` expression and handle any errors in the `catch` clauses of a `do` statement, as described in [Error Handling](https://docs.swift.org/swift-book/LanguageGuide/ErrorHandling.html) in [The Swift Programming Language](https://docs.swift.org/swift-book/) and `About Imported Cocoa Error Parameters`.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)