Skip to content

Instantly share code, notes, and snippets.

@willard1218
Created October 18, 2017 01:31
Show Gist options
  • Save willard1218/a95f6e0b784823cbf9a5df29482b4ddd to your computer and use it in GitHub Desktop.
Save willard1218/a95f6e0b784823cbf9a5df29482b4ddd to your computer and use it in GitHub Desktop.
GifEncoder
//
// ViewController.m
// GifEncoderTest
//
// Created by willard on 2017/10/18.
// Copyright © 2017年 willard. All rights reserved.
//
#import "ViewController.h"
#import <Photos/Photos.h>
#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *images = [NSMutableArray arrayWithCapacity:3];
NSMutableArray *timeIntervals = [NSMutableArray arrayWithCapacity:3];
for (NSUInteger i = 0 ; i < 3; i++) {
NSString *imageFileName = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"number_%ld", i+1] ofType:@"png"];
UIImage *image = [UIImage imageWithContentsOfFile:imageFileName];
[images addObject:image];
[timeIntervals addObject:@1];
}
[self makeAnimatedGif:images.copy timeIntervals:timeIntervals.copy fileName:@"fff.gif"];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)makeAnimatedGif:(NSArray <UIImage *>*)images
timeIntervals:(NSArray <NSNumber *>*)timeIntervals
fileName:(NSString *)fileName {
NSUInteger kFrameCount = images.count;
NSDictionary *fileProperties = @{
(__bridge id)kCGImagePropertyGIFDictionary: @{
(__bridge id)kCGImagePropertyGIFLoopCount: @0, // 0 means loop forever
}
};
NSURL *path = [self getFullPathWithName:fileName];
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)path, kUTTypeGIF, kFrameCount, NULL);
CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)fileProperties);
for (NSUInteger i = 0; i < kFrameCount; i++) {
NSDictionary *frameProperties = @{
(__bridge id)kCGImagePropertyGIFDictionary: @{
(__bridge id)kCGImagePropertyGIFDelayTime: timeIntervals[i], // a float (not double!) in seconds, rounded to centiseconds in the GIF data
}
};
@autoreleasepool {
UIImage *image =[images objectAtIndex:i]; //Here is the change
NSData *imgData = UIImagePNGRepresentation(image); //1 it represents the quality of the image.
CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties);
}
}
if (!CGImageDestinationFinalize(destination)) {
NSLog(@"failed to finalize image destination");
}
CFRelease(destination);
}
- (NSURL *)getFullPathWithName:(NSString *)fileName {
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
NSURL *fileURL = [documentsDirectoryURL URLByAppendingPathComponent:fileName];
NSLog(@"%@",documentsDirectoryURL.path);
return fileURL;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment