将Gif图像转换为NSData
我的相册中有一张gif图像.使用UIImagePickerController
选择该图像时,需要将图像转换为NSData
进行存储.
I have a gif image in my photo album. When I use the UIImagePickerController
to select that image, I need to convert the image to NSData
for storing.
我以前使用过
NSData *thumbData = UIImageJPEGRepresentation(thumbnail, 0.5);
,但不适用于gif图像. thumbData
将为零.
but it will not work with gif images. thumbData
will be nil.
-
如何从gif图像中获取
NSData
?
我怎么知道这是需要特殊处理的gif图像?
How can I know that it is a gif image that needs special handing?
此处的关键是将GIF文件或URL下载直接保存到NSData中,而不是使其成为UIImage.绕过UIImage将使GIF文件保留动画.
The key here is to save the GIF file or URL download directly into a NSData instead of making it a UIImage. Bypassing UIImage will let the GIF file keep the animation.
下面是一些将GIF文件转换为NSData的代码:
Here is some code to convert a GIF file into NSData:
NSString *filePath = [[NSBundle mainBundle] pathForResource: @"gifFileName" ofType: @"gif"];
NSData *gifData = [NSData dataWithContentsOfFile: filePath];
但是,老实说,您应该真正考虑完全不使用GIF.
But in all honesty, you should really consider not using GIF at all.