Advertisement
Guest User

Untitled

a guest
May 30th, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. #import <UIKit/UIKit.h>
  2. #import <ImageIO/ImageIO.h>
  3. #import <MobileCoreServices/MobileCoreServices.h>
  4.  
  5. static UIImage *frameImage(CGSize size, CGFloat radians) {
  6. UIGraphicsBeginImageContextWithOptions(size, YES, 1); {
  7. [[UIColor whiteColor] setFill];
  8. UIRectFill(CGRectInfinite);
  9. CGContextRef gc = UIGraphicsGetCurrentContext();
  10. CGContextTranslateCTM(gc, size.width / 2, size.height / 2);
  11. CGContextRotateCTM(gc, radians);
  12. CGContextTranslateCTM(gc, size.width / 4, 0);
  13. [[UIColor redColor] setFill];
  14. CGFloat w = size.width / 10;
  15. CGContextFillEllipseInRect(gc, CGRectMake(-w / 2, -w / 2, w, w));
  16. }
  17. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  18. UIGraphicsEndImageContext();
  19. return image;
  20. }
  21.  
  22. static void makeAnimatedGif(void) {
  23. static NSUInteger kFrameCount = 16;
  24.  
  25. NSDictionary *fileProperties = @{
  26. (__bridge id)kCGImagePropertyGIFDictionary: @{
  27. (__bridge id)kCGImagePropertyGIFLoopCount: @0, // 0 means loop forever
  28. }
  29. };
  30.  
  31. NSDictionary *frameProperties = @{
  32. (__bridge id)kCGImagePropertyGIFDictionary: @{
  33. (__bridge id)kCGImagePropertyGIFDelayTime: @0.02f, // a float (not double!) in seconds, rounded to centiseconds in the GIF data
  34. }
  35. };
  36.  
  37. NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
  38. NSURL *fileURL = [documentsDirectoryURL URLByAppendingPathComponent:@"animated.gif"];
  39.  
  40. CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypeGIF, kFrameCount, NULL);
  41. CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)fileProperties);
  42.  
  43. for (NSUInteger i = 0; i < kFrameCount; i++) {
  44. @autoreleasepool {
  45. UIImage *image = frameImage(CGSizeMake(300, 300), M_PI * 2 * i / kFrameCount);
  46. CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties);
  47. }
  48. }
  49.  
  50. if (!CGImageDestinationFinalize(destination)) {
  51. NSLog(@"failed to finalize image destination");
  52. }
  53. CFRelease(destination);
  54.  
  55. NSLog(@"url=%@", fileURL);
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement