Return to Snippet

Revision: 66405
at April 30, 2014 18:06 by sidneydekoning


Initial Code
-(void) addSpriteImage:(UIImage *) image
       withContentRect:(CGRect) rect
               toLayer:(CALayer *) layer
{
    //set image
    layer.contents = (__bridge id)image.CGImage;
    //scale contents to fit
    layer.contentsGravity = kCAGravityResizeAspect;
    //set contentRect to appropriate place in image
    layer.contentsRect = rect;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIImage *image = [UIImage imageNamed:@"spritesheet.png"];
    
    [self addSpriteImage:image withContentRect:CGRectMake(0, 0, 0.5, 0.5) toLayer:self.someUIView.layer];
    
    [self addSpriteImage:image withContentRect:CGRectMake(0.5, 0, 0.5, 0.5) toLayer:self.someUIView.layer];
    
    [self addSpriteImage:image withContentRect:CGRectMake(0, 0.5, 0.5, 0.5) toLayer:self.someUIView.layer];
    
    [self addSpriteImage:image withContentRect:CGRectMake(0.5, 0, 0.5, 0.5) toLayer:self.someUIView.layer];

}

Initial URL

                                

Initial Description
Very simple use of an non-animating spritesheet for iOS

Initial Title
Simple Spritesheet

Initial Tags
object, images

Initial Language
Objective C