【问题标题】:GIF do not animate when share on twitter在 Twitter 上分享时,GIF 不动画
【发布时间】:2014-09-19 03:27:51
【问题描述】:

我正在使用 NSArrayUIImage 创建动画 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);
    }
}];

HJImagesToGIFthis gist 帮助很大!

更新:

分析 2 个 GIF 的图像标题: 1- 图片标题:GIF89a - 通过 safari 下载并在 twitter 应用中显示 GIF 标签 2- 图片标题:GIF87a - 通过代码创建,在 twitter 应用中不显示 GIF 标签

所以图片标题!嗯..我该如何改变它?

【问题讨论】:

  • 你能告诉我这是如何工作的吗,我有同样的问题..

标签: ios image gif


【解决方案1】:

好的,问题已解决,我不确定它是否是最佳解决方案,希望任何人提供更好的解决方案!

问题确实与我通过代码创建的 GIF 的图像标题有关。 我拿了一个 GIF89a 标头 GIF 并复制了标头(前 6 个字节)并用它替换了我的 GIF 图像标头。现在在 twitter 以及电子邮件和 iMessage 上工作正常。

这是我使用的代码:

NSMutableData *gifData = [NSMutableData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"1.gif" ofType:nil]]; // GIF89a file
NSMutableData *data = [NSMutableData dataWithContentsOfFile:path]; // My created GIF file 
NSMutableData *gif89 = [NSMutableData dataWithData:[gifData subdataWithRange:NSMakeRange(0, 6)]]; // the first 6 bytes we needed (or the Header)
[data replaceBytesInRange:NSMakeRange(0, 6) withBytes:gif89.bytes]; // replace the header.. Voila! 

【讨论】:

  • 您能否详细解释一下您是如何获取标头并进行字节交换的。
  • 用代码示例更新了我的答案
  • 我按照你说的做了,除了 Facebook 和 Twitter,它在任何地方都可以正常工作。那里还有静止图像,有什么想法吗?
  • AFAIK facebook 不会在帖子中显示动画 GIF,但 twitter 会!你在推特上的图片上没有看到“GIF”标记吗?正如我在屏幕截图中所示。在一些在线工具上检查文件的标题。让我给你一个网址。
  • 所以我尝试了这个:我将 gif 发送到我的电子邮件。然后在我的 Mac 上下载并尝试通过 tweetbot 发布它并且它正在动画(与 Web 客户端相同)。通过 UIActivityViewController -> Twitter 共享的相同图像,没有动画。
猜你喜欢
  • 1970-01-01
  • 2020-10-18
  • 1970-01-01
  • 1970-01-01
  • 2014-02-19
  • 2014-09-12
  • 2016-12-23
  • 2014-12-24
  • 2018-10-13
相关资源
最近更新 更多