在Twitter上分享时,GIF不会animation

我使用UIImage NSArray创buildanimationGIF文件。 并保存在NSDocumentDirectory以及在iPhone的相册。

我面临的问题是,当我在Email Composer或iMessage中打开保存的GIF时,它的animation效果很好。 但是当我通过Twitter分享它时,它不会生成animation。 我尝试通过iPhone上的twitter应用程序分享,看到一个有趣的事情(下面的ScreenShot),我使用Safari从不同网站下载的GIF在GIF的左上angular显示一个标签“GIF”。 而我保存的GIF不。 这里可能是什么问题? 任何人都可以请指导我。

在这里输入图像说明

以下是创buildGIF文件的代码:

 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); } 

并保存到相册以及在NSDocumentDirectory的GIF。 要保存在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是大的帮助,以及这个要点 !

更新:

在分析2个GIF的图像标题:1-图像标题: GIF89a – 通过Safari浏览器下载,并在Twitter应用程序中显示GIF标签2- Image Header: GIF87a – 通过代码创build,不显示在Twitter应用程序中的GIF标签

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

好吧,所以解决了这个问题,我不确定它是否是最好的解决scheme,并希望得到更好的解决scheme!

这个问题确实与我通过代码创build的GIF图像头相关。 我带了一个GIF89a头文件GIF并复制了头文件(前6个字节),并用它replace了我的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!