在Twitter上分享时,GIF不会制作动画 [英] GIF do not animate when share on twitter
问题描述
我正在使用 NSArray
创建 UIImage
的动画GIF文件。并将其保存在 NSDocumentDirectory
以及iPhone的相册中。
I am creating animated GIF file with NSArray
of UIImage
. And saving it in NSDocumentDirectory
as well as in Photo Album of iPhone.
我面临的问题是,当我在Email Composer或iMessage中打开保存的GIF,它动画很好。但是,当我通过推特分享它时,它没有动画。我尝试通过iPhone上的Twitter应用程序分享并看到一个有趣的事情(下面的ScreenShot)我使用Safari从不同网站下载的GIF在GIF的左上角显示标签'GIF'。虽然我保存的GIF没有。这可能是什么问题?任何人都可以指导我。
The problem I am facing is, when I open the saved GIF in Email Composer or in iMessage, it animates fine. But when i share it through twitter, it does not animate. I tried sharing through twitter app on iPhone and saw an Interesting thing(ScreenShot below) that The GIFs i downloaded from different websites using Safari shows a label 'GIF' on top left corner of GIF. While my saved GIFs does not. What might be the problem here? Can anyone please guide me.
以下是创建GIF文件的代码:
Here is the code for creating the GIF file:
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的相册中,请输入以下代码:
And saving the GIF into Photo Album as well as in NSDocumentDirectory. To save in Photo Album of iPhone, here is the code:
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 是一个很大的帮助,以及这个要点!
HJImagesToGIF was big help, as well as this gist!
更新:
在分析2个GIF的图像标题时:
1-图像标题: GIF89a - 通过safari下载并在Twitter应用中显示GIF标签
2-图像标题: GIF87a - 通过代码创建,不显示GIF标签在Twitter应用程序
On analysing the Image Header of 2 GIFs: 1- Image Header: GIF89a - downloaded through safari and shows GIF label in twitter app 2- Image Header: GIF87a - Created through code, doesnt show GIF label in twitter app
所以图像标题!嗯..我该怎么改呢?
So the Image Header! hmmm.. how do I change it?
推荐答案
好的,所以解决了这个问题,我不确定它是不是最好的解决方案,并希望得到更好的解决方案!
Ok so solved the issue, I am not sure if its the best solution, and would appreciate better solutions from anyone!
这个问题确实与我通过Code创建的GIF图像标题有关。
我拿了一个GIF89a标头GIF并复制了标题(前6个字节)并用它替换了我的GIF图像标题。现在在推特以及电子邮件和iMessage上正常工作。
The problem was indeed related to the image header of the GIFs which I created through Code. I took a GIF89a header GIF and copied the header (first 6 bytes) and replaced my GIF's image header with it. Working fine now on twitter as well as email and 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!
这篇关于在Twitter上分享时,GIF不会制作动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!