# SDWebImage4.x加载GIF图片的方法

**URL:** <https://www.shiqidu.com/t/topic/549>\
**Category:** 开发调优\
**Tags:** C-C-Obj-C\
**Created:** [February 15, 2019, 12:20am UTC](https://www.shiqidu.com/t/topic/549 "2019-02-15T00:20:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leon](https://www.shiqidu.com/user_avatar/www.shiqidu.com/leon/32/5_2.png) [@Leon](https://www.shiqidu.com/u/Leon)\
**Post date:** [February 15, 2019, 12:20am UTC](https://www.shiqidu.com/t/topic/549/1 "2019-02-15T00:20:44Z")

</div>

首先需要安装下面的两个包

```bash
pod 'SDWebImage/GIF'
pod 'FLAnimatedImage'

```

然后代码中引入这两个头文件

```oc
#import <FLAnimatedImageView.h>
#import <FLAnimatedImageView+WebCache.h>

```

加载gif图片需要使用FLAnimatedImageView

```oc
// imageview
NSString *originalImagePath = [WEBSITE_PREFIX stringByAppendingPathComponent:self.imageItem[@"original"]];
UIImageView *imageView = [[UIImageView alloc] init];
if ([originalImagePath hasSuffix:@".gif"]) {
    imageView = [[FLAnimatedImageView alloc] init];
}
self.imageView = imageView;

// SQDLog(@"%@", originalImagePath);
[imageView sd_setImageWithURL:[NSURL URLWithString:originalImagePath] completed:^(UIImage *_Nullable image, NSError *_Nullable error, SDImageCacheType cacheType, NSURL *_Nullable imageURL) {
    if (!image) {
        [SVProgressHUD showErrorWithStatus:@"图片下载失败"];
        return;
    }
    self.saveBtn.enabled = YES;
}];

```
