Core Animation Snippets

by Matt Long

Here are some snippets to help with performing different tasks using Core Animation.


  • Convert NSImage to CGImageRef
    - (CGImageRef)nsImageToCGImageRef:(NSImage*)image;
        NSData * imageData = [image TIFFRepresentation];
        CGImageRef imageRef;
        if(!imageData) return nil;
        CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)imageData, NULL);
        imageRef = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
        return imageRef;


  • Scale Up Five Times
    CATransform3D transform = CATransform3DMakeScale(5.0f, 5.0f, 5.0f);
    [layer setTransform:transform];
  • Scale Down to Half Size
    CATransform3D transform = CATransform3DMakeScale(0.5f, 0.5f, 0.5f);
    [layer setTransform:transform];