【发布时间】:2016-05-04 17:14:51
【问题描述】:
我尝试用图像创建 GIF 的代码:
+ (NSData *)createGifDataWithImages:(NSArray *)images duration:(CGFloat)duration withFileName:(NSString *)fileName
{
CGFloat durationPadding = duration/((CGFloat)images.count);
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:fileName];
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path],
kUTTypeGIF,
images.count,
NULL);
NSDictionary *frameProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:@(durationPadding) forKey:(NSString *)kCGImagePropertyGIFDelayTime]
forKey:(NSString *)kCGImagePropertyGIFDictionary];
NSDictionary *gifProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount]
forKey:(NSString *)kCGImagePropertyGIFDictionary];
[images enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
UIImage *image = (UIImage *)obj;
CGImageDestinationAddImage(destination, image.CGImage, (CFDictionaryRef)frameProperties);
}];
CGImageDestinationSetProperties(destination, (CFDictionaryRef)gifProperties);
CGImageDestinationFinalize(destination);
CFRelease(destination);
NSLog(@"animated GIF file created at %@", path);
return [NSData dataWithContentsOfFile:path];
}
它最后确实创建了一个 GIF,但图像看起来像这样:
【问题讨论】:
标签: ios objective-c uiimage gif