Convert a UIImage into NSData BUT stay as a GIF file

假装没事ソ 提交于 2019-12-18 13:36:43

问题


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?


回答1:


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.




回答2:


Once it's a UIImage, it's not a gif anymore. It's already been interpreted into a core graphics image object, wrapped in the UIKit trimmings of UIImage. You DON'T have a UIImage that "is a GIF". It may have been once, but that time has passed.

If you're downloading this thing (which I assume by your comment "I don't have access to the file"), then you should download it as data, and keep it as data, and if you make a UIImage based on that data, you maybe need to keep the data around.

(This is all a guess, though, because you're dribbling out details about your question drip by drip. If you could say in a bigger breath what you want to accomplish, we can probably help better.)




回答3:


You could load it into a NSData* directly if you know the filepath. Such as:

NSData *gifData = [NSData dataWithContentsOfFile:@"myCoolPic.gif"];

NSData dataWithContentsOfFile docs



来源:https://stackoverflow.com/questions/7420229/convert-a-uiimage-into-nsdata-but-stay-as-a-gif-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!