加载GiF动画

前言:需求上的需要,UI这边给了一张GIF动画,让做为动图按钮加载出来。 加载GIF动画的办法有非常多种,下面简单写一下自己的做法。

一、使用SDWebImaged的GIF扩展类加载本地Gif文件

#import "gifViewController.h"
#import "UIImage+GIF.h"

@interface gifViewController ()
@property (nonatomic, strong) UIImageView *loadingGifImage;
@end

@implementation gifViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self loadingGifImageView];
}

- (void)loadingGifImageView{
    NSString  *name = @"gif.gif";
    NSString  *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:name ofType:nil];
    NSData  *imageData = [NSData dataWithContentsOfFile:filePath];

    if (!self.loadingGifImage) {
        self.loadingGifImage = [[UIImageView alloc]init];
    }

    self.loadingGifImage.backgroundColor = [UIColor clearColor];
    self.loadingGifImage.image = [UIImage sd_animatedGIFWithData:imageData];
    self.loadingGifImage.frame = CGRectMake(0, 0, ScreenWidth, ScreenWidth*250.0/750.0);
    [self.view addSubview:self.loadingGifImage];
    [self.view bringSubviewToFront:self.loadingGifImage];
}
@end

二、加载由帧序列组成的本地GIF动画

#import "gifViewController.h"

@interface gifViewController ()
@property (nonatomic, strong) UIImageView *loadingImageView;
@end

@implementation gifViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self loadingAnimation];
}

- (void)loadingAnimation {
    [self.view addSubview:self.loadingImageView];
    [self.loadingImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(40.0, 40.0));
    }];
}

- (UIImageView *)loadingImageView {
    if (!_loadingImageView) {
        NSMutableArray *imgArray = [[NSMutableArray alloc] init];
        for (int i = 0; i<24; i++) {
            NSString *name = [NSString stringWithFormat:@"loading_%05d@3x.png", i]; //例如:loading_00000@3x.PNG
            NSString *imageName = [@"LoadingAnimation.bundle" stringByAppendingPathComponent:name];
            UIImage *image = [UIImage imageNamed:imageName];
            [imgArray addObject:image];
        }
        _loadingImageView = [[UIImageView alloc] init];
        _loadingImageView.animationImages = imgArray;
        _loadingImageView.animationDuration = 1.0;
        [self.loadingImageView startAnimating];
    }
    return _loadingImageView;
}
@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity 默认不支持 GIF 格式的动画,但是可以通过导入第三方插件来实现 GIF 动画加载和播放。 以下是两个比较常用的插件: 1. GifDecoder:可以在 Unity 中直接加载 GIF 文件,并返回一个 Texture2D 对象,可以将其赋值给 RawImage 或者 Sprite 等组件来实现播放。GifDecoder 插件的 GitHub 地址为:https://github.com/DataB4/GifDecoder。 2. SimpleGif:是一个功能强大的 GIF 解码库,支持解码、播放和编码 GIF 文件,并且提供了丰富的 API 接口供用户使用。SimpleGif 插件的 GitHub 地址为:https://github.com/marijnz/SimpleGif。 这里以 GifDecoder 插件为例,演示如何加载 GIF 动画: 1. 从 GitHub 上下载 GifDecoder 插件,并将其导入到 Unity 项目中。 2. 在需要加载 GIF 动画的场景中,创建一个 RawImage 对象,并将其命名为 gifImage。 3. 编写以下代码: ```csharp using UnityEngine; using System.Collections; using GifDecoderNamespace; public class GifDecoderTest : MonoBehaviour { public Texture2D[] gifFrames; public float fps = 10f; void Start () { // 从 Resources 文件夹中加载 GIF 文件 TextAsset gifFile = Resources.Load("yourGifFile") as TextAsset; // 调用 GifDecoder 插件解码 GIF 文件 GifDecoder gifDecoder = new GifDecoder(); gifDecoder.Decode(gifFile.bytes); // 获取 GIF 动画的帧数 int frameCount = gifDecoder.GetFrameCount(); gifFrames = new Texture2D[frameCount]; for (int i = 0; i < frameCount; i++) { // 获取指定帧的 Texture2D 对象 gifFrames[i] = gifDecoder.GetFrameTexture(i); } // 播放 GIF 动画 StartCoroutine(PlayGifAnimation()); } IEnumerator PlayGifAnimation() { while (true) { // 遍历 GIF 动画的所有帧 for (int i = 0; i < gifFrames.Length; i++) { // 将当前帧显示在 RawImage 组件中 RawImage gifImage = GetComponent<RawImage>(); gifImage.texture = gifFrames[i]; // 等待一定时间后切换到下一帧 yield return new WaitForSeconds(1f / fps); } } } } ``` 根据需要,你可以调整代码中的参数来控制 GIF 动画的播放速度和循环方式等。请将 `yourGifFile` 替换为实际的 GIF 文件名。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值