将Gif图像转换为NSData [英] Convert Gif image to NSData

查看:206
本文介绍了将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.

  1. 如何从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.

这篇关于将Gif图像转换为NSData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆