SDWebImage播放GIF图

播放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

posted @ 2015-11-10 18:19  蓝鹰ios  阅读(7648)  评论(0编辑  收藏  举报