IOS加载Gif图片的N种方式 By-H罗

1.系统UIImageView 多张图片组成动画

/**   *   UIImageView 动画   *   Memory-23M   */  -(void)gifPlay1  {  //    NSArray  *array=@[@"image0.png",@"image1.png",@"image2.png"];  //    UIImageView  *imgview= [UIImageView imageViewAnimation:CGRectMake(50,80, 550/2, 200)  imageNames:array duration:1];            UIImageView* animatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(50,80, 550/2, 200)];      animatedImageView.animationImages =@[[UIImage imageNamed:@"image0"],                                           [UIImage imageNamed:@"image1"],                                           [UIImage imageNamed:@"image2"],                                           ];      animatedImageView.animationDuration = 1.0f;      animatedImageView.animationRepeatCount = 0;      [self.view addSubview: animatedImageView];      [animatedImageView startAnimating];  }  

 

2.利用第三方库

1)IImageView-PlayGIF  YFGIFImageView

/**   *  UIImageView-PlayGIF 是 UIImageView 子类,用来显示 GIF。UIIMageView-PlayGIF 性能高,而且占用的内存很低。   *  https://github.com/yfme/UIImageView-PlayGIF   *  Memory-21.9M   *  #import "YFGIFImageView.h"   */  -(void)gifPlay2  {      NSString  *gifPath=[[NSBundle mainBundle] pathForResource:@"test" ofType:@"gif"];      YFGIFImageView  *gifview=[[YFGIFImageView alloc]init];      gifview.backgroundColor=[UIColor clearColor];      gifview.gifPath=gifPath;      gifview.frame=CGRectMake(50, 100,550/2, 200);      [self.view addSubview:gifview];      [gifview startGIF];  }  

 

 

2)SCGIFImageView

/**   *  摘要: SCGIFImageView是一个开源的GIF图片动画显示控件,通过将GIF的每一帧都取出来生成UIImage对象存放在一个数组中,然后使用NSTimer进行动画轮转。   *  https://github.com/shichangone/SCGifExample   *  Memory-22.5M   *  #import "SCGIFImageView.h"   */  -(void)gifPlay3  {      NSString* filePath = [[NSBundle mainBundle] pathForResource:@"test.gif" ofType:nil];      NSData* imageData = [NSData dataWithContentsOfFile:filePath];      SCGIFImageView* gifImageView = [[SCGIFImageView alloc]init];      [gifImageView setData:imageData];      gifImageView.frame = CGRectMake(50,100, gifImageView.image.size.width, gifImageView.image.size.height);      [self.view addSubview:gifImageView];  }  

 

3)YLGIFImage

/**   *  YLGIFImage 是异步 GIF 图像解码器和图像查看器,支持播放 GIF 图像,而且使用很少的内存。   *  https://github.com/liyong03/YLGIFImage   *  Memory-22.7M   *  #import "YLImageView.h"   *  #import "YLGIFImage.h"   */  -(void)gifPlay5  {      YLImageView* imageView = [[YLImageView alloc] initWithFrame:CGRectMake(0, 160, 320, 240)];      [self.view addSubview:imageView];      imageView.image = [YLGIFImage imageNamed:@"test.gif"];  }  

 

 

4)SDWebImageView里的UIImage+GIF

提供接口:

+ (UIImage *)sd_animatedGIFNamed:(NSString *)name;
+ (UIImage *)sd_animatedGIFWithData:(NSData *)data;
- (UIImage *)sd_animatedImageByScalingAndCroppingToSize:(CGSize)size; 

/**   *  利用SDWebImageView 库播放gif   *  Memory-22.6M   *  #import "UIImage+GIF.h"   */  -(void)gifPlay6  {      UIImage  *image=[UIImage sd_animatedGIFNamed:@"test"];      UIImageView  *gifview=[[UIImageView alloc]initWithFrame:CGRectMake(50,80,image.size.width, image.size.height)];      gifview.backgroundColor=[UIColor orangeColor];      gifview.image=image;      [self.view addSubview:gifview];  }  

 

 

为MBProgressHUD 添加加载动画

/**   *  MBProgressHUD 添加加载动画   *  Memory-23.8M   *  #import "UIImage+GIF.h"   *  #import "MBProgressHUD.h"   */  -(void)gifPlay6  {      UIImage  *image=[UIImage sd_animatedGIFNamed:@"test"];      UIImageView  *gifview=[[UIImageView alloc]initWithFrame:CGRectMake(0,0,image.size.width/2, image.size.height/2)];      gifview.image=image;            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];      hud.color=[UIColor grayColor];//默认颜色太深了      hud.mode = MBProgressHUDModeCustomView;      hud.labelText = @"加载中...";      hud.customView=gifview;  }  

 

其它

微博客户端 VVebo 的作者开源了他自己为VVebo写的GIF解决方案 VVeboImageView, 占用内存很小。(iOS移动开发周报-第4期)

  • 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、付费专栏及课程。

余额充值