Skip to content

Instantly share code, notes, and snippets.

@whipsterCZ
Forked from ksm/gist:1869823
Created February 25, 2013 23:07
Show Gist options
  • Save whipsterCZ/5034179 to your computer and use it in GitHub Desktop.
Save whipsterCZ/5034179 to your computer and use it in GitHub Desktop.
/*
Source: Apple Developer - Understanding iOS View Compositing
*/
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
UIImage *image = [self loadImage];
if (image != nil) {
CGSize s = image.size;
CGRect r = layer.bounds;
CGFloat scale = MIN(r.size.width / s.width, r.size.height / s.height);
s.width *= scale; s.height *= scale;
r.origin.x += (r.size.width - s.width) * .5;
r.size.width = s.width;
r.origin.y += (r.size.height - s.height) * .5;
r.size.height = s.height;
CGContextSaveGState(ctx);
CGContextDrawImage(ctx, r, image.CGImage);
CGContextRestoreGState(ctx);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment