Skip to content

Instantly share code, notes, and snippets.

@yonglam
Created February 11, 2019 02:38
Show Gist options
  • Save yonglam/5314f7f65226e06ad964f0a323c85126 to your computer and use it in GitHub Desktop.
Save yonglam/5314f7f65226e06ad964f0a323c85126 to your computer and use it in GitHub Desktop.
生成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