Simple Spritesheet


/ Published in: Objective C
Save to your folder(s)

Very simple use of an non-animating spritesheet for iOS


Copy this code and paste it in your HTML
  1. -(void) addSpriteImage:(UIImage *) image
  2. withContentRect:(CGRect) rect
  3. toLayer:(CALayer *) layer
  4. {
  5. //set image
  6. layer.contents = (__bridge id)image.CGImage;
  7. //scale contents to fit
  8. layer.contentsGravity = kCAGravityResizeAspect;
  9. //set contentRect to appropriate place in image
  10. layer.contentsRect = rect;
  11. }
  12.  
  13. - (void)viewDidLoad
  14. {
  15. [super viewDidLoad];
  16.  
  17. UIImage *image = [UIImage imageNamed:@"spritesheet.png"];
  18.  
  19. [self addSpriteImage:image withContentRect:CGRectMake(0, 0, 0.5, 0.5) toLayer:self.someUIView.layer];
  20.  
  21. [self addSpriteImage:image withContentRect:CGRectMake(0.5, 0, 0.5, 0.5) toLayer:self.someUIView.layer];
  22.  
  23. [self addSpriteImage:image withContentRect:CGRectMake(0, 0.5, 0.5, 0.5) toLayer:self.someUIView.layer];
  24.  
  25. [self addSpriteImage:image withContentRect:CGRectMake(0.5, 0, 0.5, 0.5) toLayer:self.someUIView.layer];
  26.  
  27. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.