Skip to content

Instantly share code, notes, and snippets.

@anzfactory
Last active March 27, 2016 08:11
Show Gist options
  • Save anzfactory/8c28524f1e31dafcdbf3 to your computer and use it in GitHub Desktop.
Save anzfactory/8c28524f1e31dafcdbf3 to your computer and use it in GitHub Desktop.
複数画像からGIFをつくる
// フレームワークを2つインポート
// #import <ImageIO/ImageIO.h> -> ImageIO.framework
// #import <MobileCoreServices/MobileCoreServices.h> -> MobileCoreService.framework
+ (NSData *) generateGifDataWithImages:(NSArray *)images
{
// 何秒間隔で画像を切り替えるか
NSDictionary *frameProperties =
@{ (NSString *)kCGImagePropertyGIFDictionary: @{ (NSString *)kCGImagePropertyGIFDelayTime : @(1) } };
// ループのカウント(0で無限ループ)
NSDictionary *gifProperties =
@{ (NSString *)kCGImagePropertyGIFDictionary : @{ (NSString *)kCGImagePropertyGIFLoopCount : @0 } };
long frameCount = [images count];
CFMutableDataRef gifData = CFDataCreateMutable(kCFAllocatorDefault, 0);
CGImageDestinationRef destination = CGImageDestinationCreateWithData(gifData, kUTTypeGIF, frameCount, NULL);
// 渡ってきたimage分まわす
UIImage *image;
for (image in images) {
CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties);
}
CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)gifProperties);
CGImageDestinationFinalize(destination);
// nsdataに変換
NSData *result = [NSData dataWithData:( NSData *)CFBridgingRelease(gifData)];
// 解放処理
CFRelease(destination);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment