Skip to content

Instantly share code, notes, and snippets.

@oliverbarreto
Created May 2, 2013 20:37
Show Gist options
  • Save oliverbarreto/5505224 to your computer and use it in GitHub Desktop.
Save oliverbarreto/5505224 to your computer and use it in GitHub Desktop.
ObjectiveC CA Load image and with sprites
NSString *path = [[NSBundle mainBundle] pathForResource:@"yourImage.png" ofType:nil];
CGImageRef img = [UIImage imageWithContentsOfFile:path].CGImage;
CALayer *layer = [CALayer layer];
layer.contents = (id)img;
layer.bounds = CGRectMake( 0, 0, CGImageGetWidth(img), CGImageGetHeight(img) );
// with sprites atlasses
CALayer *layer = [CALayer layer];
layer.contents = (id)img;
CGSize size = CGSizeMake( 160, 198 ); // size in pixels of one frame
CGSize normalizedSize = CGSizeMake( size.width/CGImageGetWidth(img), size.height/CGImageGetHeight(img) );
layer.bounds = CGRectMake( 0, 0, size.width, size.height );
layer.contentsRect = CGRectMake( 0, 0, normalizedSize.width, normalizedSize.height );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment