Core Animation Snippets

by Matt Long

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

Images

  • Convert NSImage to CGImageRef
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    - (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;
    }

Scaling

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