iOS gif製作
阿新 • • 發佈:2019-02-18
1、建立影象目標
- CGImageDestinationRef destination;
2、建立輸出路徑(儲存的路徑)
- /*
- path
- */
3、建立CFURLRef物件
- CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)path, kCFURLPOSIXPathStyle, NO);
4、通過一個url返回影象目標
-
destination = CGImageDestinationCreateWithURL(url, kUTTypeGIF, mArray
5、設定gif的資訊,播放時隔事件,基本資料和delay事件
-
NSDictionary *frameProperties = [NSDictionary dictionaryWithObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:0.18],(NSString *)kCGImagePropertyGIFDelayTime, nil nil] forKey:(NSString *)kCGImagePropertyGIFDictionary];
- //設定gif資訊
- NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:2];
- [dict setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCGImagePropertyGIFImageColorMap];
- [dict setObject:(NSString *)kCGImagePropertyColorModelRGB forKey:(NSString *)kCGImagePropertyColorModel];
-
[dict setObject
- [dict setObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];
- NSDictionary *gifProperties = [NSDictionary dictionaryWithObject:dict forKey:(NSString *)kCGImagePropertyGIFDictionary];
- for (UIImage *dImg in mArray)
- {
- CGImageDestinationAddImage(destination, dImg.CGImage, (__bridge CFDictionaryRef)frameProperties);
- }
7、給gif新增資訊
- CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)gifProperties);
8、寫入gif圖
- CGImageDestinationFinalize(destination);
- CFRelease(destination);