播放GIF圖有好幾種方法
1.可以直接用ImageView一幀一幀的播放
2.可以用WebView加載一個頁面播放
.
.
.
但是它們的缺點比較明顯,會失幀,如果圖比較大多話,還有可能在屏幕比較小的設備上不能完全顯示出來,
SDWebImage提供了很好的方法,只要導入播放GIF的頭文件,只需短短的幾行代碼就可以實現。示例代碼如下:
#import "ViewController.h"
#import "UIImage+GIF.h"
@interface ViewController ()
@property (nonatomic,strong) UIImageView *loadingImageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initLoadingImageView];
}
- (void)initLoadingImageView
{
NSString *name = @"4升級.gif";
NSString *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:name ofType:nil];
NSData *imageData = [NSData dataWithContentsOfFile:filePath];
if (!self.loadingImageView) {
self.loadingImageView = [[UIImageView alloc]init];
}
self.loadingImageView.backgroundColor = [UIColor clearColor];
self.loadingImageView.image = [UIImage sd_animatedGIFWithData:imageData];
self.loadingImageView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:self.loadingImageView];
[self.view bringSubviewToFront:self.loadingImageView];
}
@end
