Skip to content

Instantly share code, notes, and snippets.

@yonglam yonglam/gif.m
Created Feb 11, 2019

Embed
What would you like to do?
生成GIF
- (void)makeAnimatedGif:(NSArray *)images filePath:(NSString *)filePath ration:(CGFloat)ration
{
NSInteger count = images.count;
CGImageDestinationRef destination;
CFURLRef url = CFURLCreateWithFileSystemPath(
kCFAllocatorDefault,
(__bridge CFStringRef)filePath,
kCFURLPOSIXPathStyle,
false);
destination = CGImageDestinationCreateWithURL(url, kUTTypeGIF, count, NULL);
if (ration <= 0) {
ration = count <= 5 ? 0.2 : 0.1;
}
NSDictionary *gifProperties = @{(__bridge NSString *) kCGImagePropertyGIFDictionary : @{
(__bridge NSString *) kCGImagePropertyGIFLoopCount : @(0),
(__bridge NSString *) kCGImagePropertyColorModel : (__bridge NSString *) kCGImagePropertyColorModelRGB,
(__bridge NSString *) kCGImagePropertyDepth : @(8),
(__bridge NSString *) kCGImagePropertyGIFHasGlobalColorMap : @(NO),
}
};
NSDictionary *frameProperties = @{(__bridge NSString *) kCGImagePropertyGIFDictionary : @{
(__bridge NSString *) kCGImagePropertyGIFDelayTime : @(ration),
}
};
CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef) gifProperties);
for (UIImage *srcImage in images)
{
uint8_t *colorTable =nil;
#ifdef kUseNewTableColor
colorTable = [self createColorTableWithImage:srcImage];
#endif
UIImage * reduceImage = [self drawImage:srcImage colorTable:colorTable];
#ifdef kUseNewTableColor
free(colorTable);
#endif
CGImageDestinationAddImage(destination, reduceImage.CGImage, (__bridge CFDictionaryRef) frameProperties);
}
CGImageDestinationFinalize(destination);
CFRelease(destination);
CFRelease(url);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.