叮叮咚咚鐺鐺凿渊,本領(lǐng)大
/**
合成gif
@param imagePathArray 圖片路徑數(shù)組
*/
- (void)composeGIF:(NSMutableArray *)imagePathArray {
//創(chuàng)建gif路徑
NSString *imagepPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString * gifPath = [imagepPath stringByAppendingString:@"/my.gif"];
NSLog(@"gifPath %@",gifPath);
//圖像目標
CGImageDestinationRef destination;
CFURLRef url=CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)gifPath, kCFURLPOSIXPathStyle, false);
//通過一個url返回圖像目標
destination = CGImageDestinationCreateWithURL(url, kUTTypeGIF, imagePathArray.count, NULL);
//設(shè)置gif的信息,播放間隔時間,基本數(shù)據(jù),和delay時間,可以自己設(shè)置
NSDictionary *frameProperties = [NSDictionary
dictionaryWithObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:0.1], (NSString *)kCGImagePropertyGIFDelayTime, nil]
forKey:(NSString *)kCGImagePropertyGIFDictionary];
//設(shè)置gif信息
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:2];
//顏色
[dict setObject:[NSNumber numberWithBool:YES] forKey:(NSString*)kCGImagePropertyGIFHasGlobalColorMap];
//顏色類型
[dict setObject:(NSString *)kCGImagePropertyColorModelRGB forKey:(NSString *)kCGImagePropertyColorModel];
//顏色深度
[dict setObject:[NSNumber numberWithInt:8] forKey:(NSString*)kCGImagePropertyDepth];
//是否重復(fù)
[dict setObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];
NSDictionary * gifproperty = [NSDictionary dictionaryWithObject:dict forKey:(NSString *)kCGImagePropertyGIFDictionary];
//合成gif
for (NSString * imagepath in imagePathArray)
{
UIImage *image=[UIImage imageWithContentsOfFile:imagepath];
CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties);
}
CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)gifproperty);
CGImageDestinationFinalize(destination);
CFRelease(destination);
}