Created
February 16, 2013 23:00
-
-
Save mayoff/4969104 to your computer and use it in GitHub Desktop.
Example of creating an animated GIF on iOS, with no 3rd-party code required. This should also be easy to port to OS X.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
#import <ImageIO/ImageIO.h> | |
#import <MobileCoreServices/MobileCoreServices.h> | |
static UIImage *frameImage(CGSize size, CGFloat radians) { | |
UIGraphicsBeginImageContextWithOptions(size, YES, 1); { | |
[[UIColor whiteColor] setFill]; | |
UIRectFill(CGRectInfinite); | |
CGContextRef gc = UIGraphicsGetCurrentContext(); | |
CGContextTranslateCTM(gc, size.width / 2, size.height / 2); | |
CGContextRotateCTM(gc, radians); | |
CGContextTranslateCTM(gc, size.width / 4, 0); | |
[[UIColor redColor] setFill]; | |
CGFloat w = size.width / 10; | |
CGContextFillEllipseInRect(gc, CGRectMake(-w / 2, -w / 2, w, w)); | |
} | |
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return image; | |
} | |
static void makeAnimatedGif(void) { | |
static NSUInteger kFrameCount = 16; | |
NSDictionary *fileProperties = @{ | |
(__bridge id)kCGImagePropertyGIFDictionary: @{ | |
(__bridge id)kCGImagePropertyGIFLoopCount: @0, // 0 means loop forever | |
} | |
}; | |
NSDictionary *frameProperties = @{ | |
(__bridge id)kCGImagePropertyGIFDictionary: @{ | |
(__bridge id)kCGImagePropertyGIFDelayTime: @0.02f, // a float (not double!) in seconds, rounded to centiseconds in the GIF data | |
} | |
}; | |
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil]; | |
NSURL *fileURL = [documentsDirectoryURL URLByAppendingPathComponent:@"animated.gif"]; | |
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypeGIF, kFrameCount, NULL); | |
CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)fileProperties); | |
for (NSUInteger i = 0; i < kFrameCount; i++) { | |
@autoreleasepool { | |
UIImage *image = frameImage(CGSizeMake(300, 300), M_PI * 2 * i / kFrameCount); | |
CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties); | |
} | |
} | |
if (!CGImageDestinationFinalize(destination)) { | |
NSLog(@"failed to finalize image destination"); | |
} | |
CFRelease(destination); | |
NSLog(@"url=%@", fileURL); | |
} |
Wish, this was in Swift. I am trying to convert, once I am done I will post it.
I implemented it in Swift 3, it's here.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Have you seen this code generate the following error when CGImageDestinationFinalize is called?
: The function `CGContextClear' is obsolete and will be removed in an upcoming update. Unfortunately, this application, or a library it uses, is using this obsolete function, and is thereby contributing to an overall degradation of system performance.