【发布时间】:2014-09-19 03:27:51
【问题描述】:
我正在使用 NSArray 或 UIImage 创建动画 GIF 文件。并将其保存在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);
}
}];
HJImagesToGIF 和this gist 帮助很大!
更新:
分析 2 个 GIF 的图像标题: 1- 图片标题:GIF89a - 通过 safari 下载并在 twitter 应用中显示 GIF 标签 2- 图片标题:GIF87a - 通过代码创建,在 twitter 应用中不显示 GIF 标签
所以图片标题!嗯..我该如何改变它?
【问题讨论】:
-
你能告诉我这是如何工作的吗,我有同样的问题..