Open Shelf…

Loading your own textures

This is something I’m asked about fairly frequently, and the answer is actually pretty simple; it involves about ten or fifteen lines of code, overall, and BackRow provides just about everything you’ll need.

Firstly, look in the BackRowUtils.h file in your project, created from the Apple TV Appliance Xcode template. There you’ll see the following declarations for some public functions in BackRow:

CGImageRef CreateImageForURL( NSURL * imageURL );
CGImageRef CreatePNGForURL( NSURL * imageURL );
CGImageRef CreateJPEGForURL( NSURL * imageURL );

The first function is a catch-all, and can handle anything that the CoreGraphics CGImageSourceRef can do. The latter two go straight to the custom sources for PNG and JPEG images respectively, so use those if you know the image type you’ll be using.

How do we put them to use? Well, first of all we need a URL. To get that from ‘MyIcon.png’ in your bundle’s Resources folder, we’ll use NSBundle to get a path string:

[[NSBundle bundleForClass: [self class]] pathForResource: @"MyIcon"
                                                  ofType: @"png"];

…which we’ll then convert into a URL:

url = [NSURL fileURLWithPath: path];

This then can be used to create a CFImageRef, which is easily done thanks to the functions provided by BackRow:

image = CreatePNGForURL( url );

We then get a context from our scene, and use that, with the image, to create an autoreleased texture object:

[BRBitmapTexture textureWithImage: image
                          context: [[self scene] resourceContext]
                           mipmap: YES];

Lastly, always remember to release your CGImageRef, like so:

CFRelease( image );

The resulting texture object can be given to any of the -setIcon: methods (those all expect a texture object), or can be used to set the contents of a BRImageControl through the -setTexture: methods.

Here’s the full working example:

NSString * path = [[NSBundle bundleForClass: [self class]]
                   pathForResource: @"MyIcon"
                            ofType: @"png"];
NSURL * iconURL = [NSURL fileURLWithPath: path];

CGImageRef image = CreatePNGForURL( iconURL );

BRRenderContext * ctx = [[self scene] resourceContext];
BRTexture * tex = [BRBitmapTexture textureWithImage: image
                                            context: ctx
                                             mipmap: YES];

CFRelease( image );

No Responses

Note that comments are displayed in reverse chronological order with topmost comments being freshest. Subscribe | Comment

Leave a Reply

You must be logged in to post a comment.