以下是 OC 代码 NSArray *document = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documenStr = [document objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *textDic= [documenStr stringByAppendingString:@"/gif"];
[fileManager createDirectoryAtPath:textDic withIntermediateDirectories:YES attributes:nil error:nil];
NSString *path = [textDic stringByAppendingString:@"zomake.gif"];
NSLog(@"path = %@",path);
CGImageDestinationRef destion;
CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)path, kCFURLPOSIXPathStyle, false);
destion = CGImageDestinationCreateWithURL(url, kUTTypeGIF, imagePathArray.count, NULL);
NSDictionary *frameDic = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.3] forKey:(NSString *)kCGImagePropertyGIFDelayTime]
forKey:(NSString *)kCGImagePropertyGIFDictionary];
NSMutableDictionary *gifParmdict = [NSMutableDictionary dictionaryWithCapacity:2];
[gifParmdict setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCGImagePropertyGIFHasGlobalColorMap];
[gifParmdict setObject:(NSString *)kCGImagePropertyColorModelRGB forKey:(NSString *)kCGImagePropertyColorModel];
[gifParmdict setObject:[NSNumber numberWithInt:8] forKey:(NSString *)kCGImagePropertyDepth];
[gifParmdict setObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];
NSDictionary *gifProperty = [NSDictionary dictionaryWithObject:gifParmdict forKey:(NSString *)kCGImagePropertyGIFDictionary];
//单帧添加到 gif
for (UIImage *dimage in imagePathArray) {
CGImageDestinationAddImage(destion, dimage.CGImage,(__bridge CFDictionaryRef)frameDic);
}
CGImageDestinationSetProperties(destion, (__bridge CFDictionaryRef)gifProperty);
CGImageDestinationFinalize(destion);
CFRelease(destion);
//此处解析 gif
NSData *data = [NSData dataWithContentsOfFile:path];
//此处将得到的 nsData 转为 gif 动画
UIImage *gifImage = [UIImage sd_tz_animatedGIFWithData:data];
望 V 友们给点思路。
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.