Search code examples
iphoneobjective-cioscocoa-touchnsdata

Convert a UIImage into NSData BUT stay as a GIF file


I have a UIImage which is a GIF file.

I need to make it make it into an NSData object. But it needs to stay a GIF so I can't use UIImageJPEGRepresentation.

Any ideas?


Solution

  • I faced the same issue you mention and here is the code i used to save the animated gif to Photos without losing animation:

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    
    NSData *data = [NSData dataWithContentsOfURL:[self getCurrentGIFURL]];
    
    [library writeImageDataToSavedPhotosAlbum:data metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {}
    

    Basically, if you save the gif from URL or from file directly as an NSData object instead of making it a UIImage, it will keep the animation.