GIF doesn’t come to life when shared on twitter

I am creating an animated GIF file with NSArray from UIImage . And save it in NSDocumentDirectory , as well as in the iPhone photo album.

The problem I am facing is that when I open a saved GIF in Email Composer or in iMessage, it animates perfectly. But when I share it via twitter, it doesn’t come to life. I tried sharing via the Twitter app on the iPhone and I saw an interesting thing (ScreenShot below) that GIFs downloaded from different sites using Safari show the “GIF” label in the upper left corner of the GIF. Although my saved GIFs do not work. What could be the problem here? Someone can guide me.

enter image description here

Here is the code to create 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); } 

And save the GIF in a photo album, as well as in NSDocumentDirectory. To save to your iPhone photo album, 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 was a big help, but this is the point !

UPDATE:

When analyzing the image header from 2 GIF files: 1- Image header: GIF89a - is downloaded via safari and shows the GIF tag in the Twitter application 2- Image header: GIF87a . Created using code, does not show GIF mark in Twitter application.

So, the title of the image! hmm .. how do i change it?

+6
source share
1 answer

Well, that’s how the problem solved, I’m not sure that this is the best solution, and I will be grateful for the best solutions from anyone!

The problem was really related to the GIF image header that I created through Code. I took the GIF89a GIF header and copied the header (first 6 bytes) and replaced its GIF image header with it. Now we work perfectly on Twitter, as well as on email and iMessage.

Here is the code I used:

 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! 
+7
source

Source: https://habr.com/ru/post/972930/


All Articles