ios 旋转加载gif_iOS-加载gif图片的几种方式

1. 原生方法:

UIWebView特点:加载速度略长,性能更优,播放的gif动态图更加流畅。

//动态展示GIF图片-WebView

-(void)showGifImageWithWebView{

//读取gif图片数据

NSData *gifData = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"earthGif" ofType:@"gif"]];

//UIWebView生成

UIWebView *imageWebView = [[UIWebView alloc] initWithFrame:CGRectMake(112, 302, 132, 102)];

//用户不可交互

imageWebView.userInteractionEnabled = NO;

//加载gif数据

[imageWebView loadData:gifData MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];

//视图添加此gif控件

[self.view addSubview:imageWebView];

}

UIImagView加载的方式更加快速,性能不如UIWebView,优点:易于扩展

增加一个UIImageView的类别(category),增加两个方法

UIImage+Tool.h

#import

@interface UIImageView (Tool)

/** 解析gif文件数据的方法 block中会将解析的数据传递出来 */

-(void)getGifImageWithUrk:(NSURL *)url returnData:(void(^)(NSArray * imageArray,NSArray*timeArray,CGFloat totalTime, NSArray* widths, NSArray* heights))dataBlock;

/** 为UIImageView添加一个设置gif图内容的方法: */

-(void)yh_setImage:(NSURL *)imageUrl;

@end

.m

@implementation UIImageView (Tool)

//解析gif文件数据的方法 block中会将解析的数据传递出来

-(void)getGifImageWithUrk:(NSURL *)url returnData:(void(^)(NSArray * imageArray, NSArray*timeArray,CGFloat totalTime, NSArray* widths,NSArray* heights))dataBlock{

//通过文件的url来将gif文件读取为图片数据引用

CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, NULL);

//获取gif文件中图片的个数

size_t count = CGImageSourceGetCount(source);

//定义一个变量记录gif播放一轮的时间

float allTime=0;

//存放所有图片

NSMutableArray * imageArray = [[NSMutableArray alloc]init];

//存放每一帧播放的时间

NSMutableArray * timeArray = [[NSMutableArray alloc]init];

//存放每张图片的宽度 (一般在一个gif文件中,所有图片尺寸都会一样)

NSMutableArray * widthArray = [[NSMutableArray alloc]init];

//存放每张图片的高度

NSMutableArray * heightArray = [[NSMutableArray alloc]init];

//遍历

for (size_t i=0; i

CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);

[imageArray addObject:(__bridge UIImage *)(image)];

CGImageRelease(image);

//获取图片信息

NSDictionary * info = (__bridge NSDictionary*)CGImageSourceCopyPropertiesAtIndex(source, i, NULL);

CGFloat width = [[info objectForKey:(__bridge NSString *)kCGImagePropertyPixelWidth] floatValue];

CGFloat height = [[info objectForKey:(__bridge NSString *)kCGImagePropertyPixelHeight] floatValue];

[widthArray addObject:[NSNumber numberWithFloat:width]];

[heightArray addObject:[NSNumber numberWithFloat:height]];

NSDictionary * timeDic = [info objectForKey:(__bridge NSString *)kCGImagePropertyGIFDictionary];

CGFloat time = [[timeDic objectForKey:(__bridge NSString *)kCGImagePropertyGIFDelayTime]floatValue];

allTime+=time;

[timeArray addObject:[NSNumber numberWithFloat:time]];

}

dataBlock(imageArray,timeArray,allTime,widthArray,heightArray);

}

//为UIImageView添加一个设置gif图内容的方法:

-(void)yh_setImage:(NSURL *)imageUrl{

__weak id __self = self;

[self getGifImageWithUrk:imageUrl returnData:^(NSArray *imageArray, NSArray *timeArray, CGFloat totalTime, NSArray *widths, NSArray *heights) {

//添加帧动画

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];

NSMutableArray * times = [[NSMutableArray alloc]init];

float currentTime = 0;

//设置每一帧的时间占比

for (int i=0; i

[times addObject:[NSNumber numberWithFloat:currentTime/totalTime]];

currentTime+=[timeArray[i] floatValue];

}

[animation setKeyTimes:times];

[animation setValues:imageArray];

[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];

//设置循环

animation.repeatCount= MAXFLOAT;

//设置播放总时长

animation.duration = totalTime;

//Layer层添加

[[(UIImageView *)__self layer]addAnimation:animation forKey:@"gifAnimation"];

}];

}

@end

在加载gif的地方使用导入 UIImageView+Tool

-(void)showGifImageWithImageView{

UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(112, 342, 132, 102)];

NSURL * url = [[NSURL alloc]initFileURLWithPath:[[NSBundle mainBundle] pathForResource:@"earthGif.gif" ofType:nil]];

[imageView yh_setImage:url];

[self.view addSubview:imageView];

}

2. 第三方:

#import "YLGIFImage.h"

#import "YLImageView.h"

-(void)showGifImageWithYLImageView{

YLImageView* imageView = [[YLImageView alloc] initWithFrame:CGRectMake(112, 342, 132, 102)];

CGFloat centerX = self.view.center.x;

[imageView setCenter:CGPointMake(centerX, 402)];

[self.view addSubview:imageView];

imageView.image = [YLGIFImage imageNamed:@"earthGif.gif"];

}

-(void)showGifImageWithFLAnimatedImage{

//GIF 转 NSData

//Gif 路径

NSString *pathForFile = [[NSBundle mainBundle] pathForResource: @"earthGif" ofType:@"gif"];

//转成NSData

NSData *dataOfGif = [NSData dataWithContentsOfFile: pathForFile];

//初始化FLAnimatedImage对象

FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:dataOfGif];

//初始化FLAnimatedImageView对象

FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc] init];

//设置GIF图片

imageView.animatedImage = image;

imageView.frame = CGRectMake(112, 342, 132, 102);

[self.view addSubview:imageView];

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: iOS 富文本可以通过使用 NSAttributedString 和 NSTextAttachment 类加载 GIF 图片,达到将富文本与动态效果结合的效果。 首先,需要将 GIF 图片转换为 NSData 格式,可通过使用 NSData 的类方法 dataWithContentsOfFile 或者 dataWithContentsOfURL 来实现。 接着,创建 NSTextAttachment 实例,并以 NSData 格式将 GIF 图片作为参数传入。然后,创建 NSMutableAttributedString 实例,并将富文本内容包含在其中。 最后,使用 UILabel、UITextView 或者 UIWebView 等组件来展示富文本内容,并加以控制动态效果的播放周期及重复次数等属性,达到更丰富的用户体验。 需要注意的是,在使用富文本加载 GIF 图片时,应考虑对应用性能及网络耗费等问题进行优化调整,使得应用流畅稳定,并能够节省用户移动数据流量等资源。 ### 回答2: iOS开发中,要加载gif动图作为富文本,可以使用如下的步骤: 1. 引入SDWebImage库:在项目中添加SDWebImage库,这是一个常用的图片加载库,可以方便地加载并显示网络上的图片。 2. 下载gif图片:从网络上找到合适的gif图片,并将其下载到本地。 3. 将gif图片添加到富文本中:使用SDWebImage提供的方法将gif图片添加到富文本中,可以使用`UIImage sd_animatedGIFWithData:`方法将本地的gif图片转换为UIImage对象,并使用该UIImage对象创建一个`NSTextAttachment`对象。 4. 将NSTextAttachment对象添加到NSAttributedString中:将创建好的NSTextAttachment对象添加到NSMutableAttributedString对象中,可以使用`appendAttributedString:`方法将其追加到NSMutableAttributedString对象的末尾。 5. 将NSAttributedString对象显示在界面上:通过UILabel、UITextView等界面控件,将NSMutableAttributedString对象显示在界面上,即可完成富文本加载gif的功能。 需要注意的是,为了保证gif动图的流畅播放,SDWebImage库会将gif图片展示为一系列的静态图片,然后再按照正确的帧率进行播放。另外,对于高性能的gif加载,也可以使用其他优化库,如FLAnimatedImage等。 综上所述,通过引入SDWebImage库、下载gif图片、将图片添加到富文本中,并将富文本显示在界面上等一系列步骤,即可实现在iOS加载gif动图作为富文本显示的功能。 ### 回答3: 在iOS中,要加载GIF图像并将其显示在富文本中,我们可以采取以下步骤: 1. 首先,我们需要获取GIF图像文件的URL或路径。可以从互联网上下载或从应用程序的资源文件中获取。例如,如果GIF图像保存在应用程序的资源文件中,则可以使用`Bundle.main.url(forResource: "myGif", withExtension: "gif")`方法获取该文件的URL。 2. 接下来,我们需要将GIF图像文件加载到`Data`对象中,以便将其与`NSAttributedString`富文本一起使用。我们可以使用`Data(contentsOf: gifURL)`方法将URL转换为Data对象。 3. 然后,我们可以创建一个`NSTextAttachment`对象,并将GIF图像数据设置为其`image`属性。例如,可以使用`NSTextAttachment(image: UIImage(data: gifData)!)`来创建`NSTextAttachment`对象。 4. 然后,我们可以使用`NSAttributedString`的`append`方法将`NSTextAttachment`对象添加到富文本中,并设置其适当的位置。例如,可以使用`attributedString.append(NSAttributedString(attachment: textAttachment))`语句将`NSTextAttachment`对象添加到`attributedString`富文本字符串中。 5. 最后,我们可以将包含GIF图像的富文本字符串应用于文本视图或标签等UI元素,以便在界面上显示GIF图像。例如,对于UILabel,可以使用`label.attributedText = attributedString`将富文本字符串应用于标签。 综上所述,以上是利用iOS富文本加载GIF的简要步骤。通过将GIF图像文件加载到`Data`对象中,然后将其作为`NSTextAttachment`对象添加到富文本字符串中,我们可以在富文本中显示GIF图像,并将其应用于iOS界面中的相应UI元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值