我正在创建动画 GIF 文件NSArray
of UIImage
。并将其保存在NSDocumentDirectory
以及 iPhone 相册中。
我面临的问题是,当我在电子邮件编辑器或 iMessage 中打开保存的 GIF 时,它的动画效果很好。但是当我通过 Twitter 分享它时,它没有动画。我尝试通过 iPhone 上的 Twitter 应用程序进行分享,发现了一个有趣的事情(下面的屏幕截图),我使用 Safari 从不同网站下载的 GIF 在 GIF 的左上角显示了一个标签“GIF”。而我保存的 GIF 却没有。这里可能有什么问题?任何人都可以指导我吗?
以下是创建 GIF 文件的代码:
float delay = 1.0/15.0; // 15 FPS
NSDictionary *prep = @{
(__bridge id)kCGImagePropertyGIFDictionary: @{
(__bridge id)kCGImagePropertyGIFDelayTime: @(delay)
}
};
// static NSUInteger kFrameCount = 16;
NSDictionary *fileProperties = @{
(__bridge id)kCGImagePropertyGIFDictionary: @{
(__bridge id)kCGImagePropertyGIFLoopCount: @0 // 0 means loop forever
}
};
CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:path];
CGImageDestinationRef dst = CGImageDestinationCreateWithURL(url, kUTTypeGIF, [images count], nil);
CGImageDestinationSetProperties(dst, (__bridge CFDictionaryRef)fileProperties);
for (int i=0;i<[images count];i++)
{
//load anImage from array
UIImage * anImage = [images objectAtIndex:i];
CGImageDestinationAddImage(dst, anImage.CGImage,(__bridge CFDictionaryRef)prep);
}
bool fileSave = CGImageDestinationFinalize(dst);
CFRelease(dst);
if(fileSave) {
NSLog(@"animated GIF file created at %@", path);
}else{
NSLog(@"error: no animated GIF file created at %@", path);
}
并将 GIF 保存到相册和 NSDocumentDirectory 中。要保存到iPhone的相册中,代码如下:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
NSData *data = [NSData dataWithContentsOfFile:tempPath];
[library writeImageDataToSavedPhotosAlbum:data metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
if (error) {
NSLog(@"Error Saving GIF to Photo Album: %@", error);
} else {
// TODO: success handling
NSLog(@"GIF Saved to %@", assetURL);
success(tempPath);
}
}];
汇嘉图片转GIF https://github.com/HarrisonJackson/HJImagesToGIF是很大的帮助,以及这个要点 https://gist.github.com/mayoff/4969104!
UPDATE:
分析 2 个 GIF 的图像标题:
1- 图片标题:GIF89a- 通过 safari 下载并在 Twitter 应用程序中显示 GIF 标签
2-图像标题:GIF87a- 通过代码创建,在 Twitter 应用程序中不显示 GIF 标签
所以图像标题!嗯..我该如何改变它?