使用SDWebImage加载GIF动画

一:加载本地GIF
使用SDWebImage加载GIF,常用的有两种方法:

+ (UIImage *)sd_animatedGIFNamed:(NSString *)name;
+ (UIImage *)sd_animatedGIFWithData:(NSData *)data;

使用起来也是非常简单

#import "ViewController.h"
#import 

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

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 调用以下两种方法都行
    [self initLoadingImageView];
//    [self initLoadingImageView1];
}

- (void)initLoadingImageView
{
    self.loadingImageView = [[UIImageView alloc] init];
    self.loadingImageView.image = [UIImage sd_animatedGIFNamed:@"yh_dongtai"];
    self.loadingImageView.frame = CGRectMake(100, 100, 150, 150);
    [self.view addSubview:self.loadingImageView];
    [self.view bringSubviewToFront:self.loadingImageView];
}

- (void)initLoadingImageView1
{
    NSString * filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"yh_dongtai" ofType:@"gif"];
    NSData * imageData = [NSData dataWithContentsOfFile:filePath];
    self.loadingImageView = [[UIImageView alloc] init];
    self.loadingImageView.image = [UIImage sd_animatedGIFWithData:imageData];
    self.loadingImageView.frame = CGRectMake(100, 100, 150, 150);
    [self.view addSubview:self.loadingImageView];
    [self.view bringSubviewToFront:self.loadingImageView];
}
@end

二:加载网络GIF文件
加载网络GIF图片就比较简单了,直接使用sd_setImageWithURL:即可。

- (void)initLoadingImageView2
{
    self.loadingImageView = [[UIImageView alloc] init];
    NSString * url = @"https://gss0.baidu.com/-Po3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/d439b6003af33a87624be4ddc75c10385243b5d7.jpg";
    self.loadingImageView sd_setImageWithURL:[NSURL URLWithString:url]];
    self.loadingImageView.frame = CGRectMake(100, 100, 150, 150);
    [self.view addSubview:self.loadingImageView];
    [self.view bringSubviewToFront:self.loadingImageView];
}

你可能感兴趣的:(使用SDWebImage加载GIF动画)