加載GIF動畫方法 iOS


方法一 使用UIWebView

_codeStr為gif網址      如果是本地的gif可以直接使用dataWithContentsOfFile方法

   NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:_codeStr]];

   UIWebView  *codeWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,45, 45)];

   [codeWebView loadData:data MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];

   [self.view addSubview:codeWebView];

該方法有一個弊端就是    gif圖的大小不能改變,顯示的就是它本來的大小。

方法二  通過SDWebImage方法 導入UIImage+GIF.h

    UIImageView *codeImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,45, 45)];

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:_codeStr]];

    UIImage *image = [UIImage sd_animatedGIFWithData:data];//實現gif圖的展示

    codeImageView.image = image;

    [self.view addSubview:codeImageView];


免責聲明!

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



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