Convert a UIImage into NSData BUT stay as a GIF file

前端 未结 3 772
既然无缘
既然无缘 2021-01-01 05:51

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.

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-01 06:16

    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.

提交回复
热议问题