SDWebimage 在4.0版本之前,調(diào)用如下的方法可以直接展示GIF的圖片
- (void)sd_setImageWithURL:(nullable NSURL *)url
placeholderImage:(nullable UIImage *)placeholder
options:(SDWebImageOptions)options
completed:(nullable SDExternalCompletionBlock)completedBlock;
但是4.0版本之后就不行了章郁,因為SDWebimage新加入了FLAnimatedImageView這個類
直接用FLAnimatedImageView創(chuàng)建一個imageview 然后再調(diào)用之前的方法就可以了
具體的實現(xiàn)步驟:
pod導(dǎo)入下面的庫
pod 'SDWebImage/GIF'
創(chuàng)建imageView
.h文件
//倒入頭文件
#import <SDWebImage/FLAnimatedImageView+WebCache.h>
@interface LSLaunchView : UIView
@property (nonatomic, weak) FLAnimatedImageView *adImageView;
@end
.m文件
- (void)addAdImageView
{
FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
[self addSubview:imageView];
//下面方法二選一 根據(jù)自己的需求
//帶占位圖的方法
NSURL *url = [NSURL URLWithString:(NSString *)object.strIcon];
[imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"icon_placeholder"]];
//帶block的方法
[_launchView.adImageView sd_setImageWithURL:url placeholderImage:nil options:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//
NSLog(@"實現(xiàn)一些方法");
} ];
}
加載本地GIF
NSString *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"圖片名稱.gif" ofType:nil];
NSData *imageData = [NSData dataWithContentsOfFile: filePath];
UIImageView *adImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, (BXScreenW-30)/2, BXScreenH/2-20)];
adImage.image = [UIImage sd_animatedGIFWithData: imageData];
[self.view addSubview: adImage];
備注:
還有一種方式 就是將pod 庫的SDWebImage回退到4.0之前的版本
pod 'SDWebImage','3.8.2'