如何将gif图片的图像转换为iOS的NSData?
在我的iOS应用程序中,我想在我的如何将gif图片的图像转换为iOS的NSData?,ios,nsdata,sdwebimage,Ios,Nsdata,Sdwebimage,在我的iOS应用程序中,我想在我的tableViewCell中显示Gif图像。通过SDWebImage下载此Gif图像,并使用FLAnimatedImageView显示此图像 但是现在我有一个问题,SDWebImage返回一个图像,但是FLAnimatedImageView需要一个NSData 如何将gif图像转换为NSData 请原谅我的英语不好 [[SDWebImageManager sharedManager] downloadImageWithURL:url options:0 prog
tableViewCell
中显示Gif图像。通过SDWebImage
下载此Gif图像,并使用FLAnimatedImageView
显示此图像
但是现在我有一个问题,SDWebImage
返回一个图像,但是FLAnimatedImageView
需要一个NSData
如何将gif
图像转换为NSData
请原谅我的英语不好
[[SDWebImageManager sharedManager] downloadImageWithURL:url options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
if (image.images.count > 0)//gif
{
// how to get the data
FLAnimatedImage *animatedImage = [FLAnimatedImage animatedImageWithGIFData:data];
_chatGIFImage.animatedImage = animatedImage1;
}
}];
如果您有图像的URL,您可以直接使用以下命令将其转换为NSData
NSData *data = [NSData dataWithContentsOfURL: imageURL];
您可以执行以下操作:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSError* error = nil;
NSData* ImageData = [NSData dataWithContentsOfURL:yourIMGUrl options:NSDataReadingUncached error:&error];
if (error) {
NSLog(@"%@", [error localizedDescription]);
} else {
NSLog(@"successfull.");
dispatch_sync(dispatch_get_main_queue(), ^{
FLAnimatedImage *animatedImage = [FLAnimatedImage animatedImageWithGIFData:ImageData];
_chatGIFImage.animatedImage = animatedImage1;
});
}
});
你可以简单地使用
// UIImageJPEGRepresentation(<#UIImage * _Nonnull image#>, <#CGFloat compressionQuality#>)
NSData *data = UIImageJPEGRepresentation(image, 1.0);
//UIImageJPEG表示法(,)
NSData*data=UIImageJPEG表示法(图像,1.0);
压缩质量可以介于0.0(高压缩,低质量)和1.0(低压缩,高质量)您可以通过使用SDwebimage直接使用gif图像libI发现“UIImage+gif.h”有一个方法:“+(UIImage*)sd_animatedGIFWithData:(NSData*)data;”如何获得反向方法?将gif图像转换为NSData。@Jigar Tarsariya我已经使用SDwebimage来显示gif图像,但它有巨大的内存泄漏,两张2M gif图像可以在线带来2亿内存泄漏,应用程序很容易崩溃,我想我的幸福生活不会再长了,我的老板会惩罚我/(ㄒoㄒ)/~~Ohhhk…然后你必须找到将gif图像转换为NSData的方法:)@Jigar Tarsariya是的,我被折磨了一天。
dataWithContentsOfURL
将阻止调用它的线程,因为这是一种同步方法。你不应该在主线程上调用此函数,因为它将在慢速网络上阻止应用程序数十秒。这不会获取GIF格式的NSData,以用于FLAnimatedImage。