以下是 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 友们给点思路。