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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM