iOS 加載gif圖片

一,UIWebview加載

核心代碼如下:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    
#pragma mark-  使用uiwebview加載gif
     /*
    NSData *data = [NSData dataWithContentsOfFile:path];
    使用loadData:MIMEType:textEncodingName: 則有警告
    [webView loadData:data MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
    */
    webView = [[UIWebView alloc] initWithFrame:CGRectZero];
    webView.delegate = self;
    NSString *path = [[NSBundle mainBundle] pathForResource:@"earth1" ofType:@"gif"];
    NSURL *url = [NSURL URLWithString:path];
    [webView loadRequest:[NSURLRequest requestWithURL:url]];
    [self.view addSubview:webView];

    //加載xib
    aaView = [LoginView loadLoginView];
    aaView.backgroundColor = [UIColor clearColor];
    [aaView setFrame:self.view.bounds];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [webView setFrame:self.view.bounds];
    
    [self.view addSubview:aaView];
}

注意:
上面的xib也就是,登錄自定義view要在webview請求完之后再加上去,否則會出現(xiàn)白屏一閃之后再到正常。(原因其實(shí)很簡單,webview請求數(shù)據(jù)是需要點(diǎn)時(shí)間的)
還有使用webview加載gif,無法設(shè)置動畫間隔時(shí)間


二,SDWebImage加載

注意SDWebImage4.0之后,加載gif方式的更新,新方法如下截圖:



示例代碼:

#import "FLAnimatedImageView.h"
#import "FLAnimatedImage.h"

FLAnimatedImageView *imgView = [FLAnimatedImageView new];
[view addSubview:imgView];
NSString* filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]]pathForResource:@"loading" ofType:@"gif"];
NSData  *imageData = [NSData dataWithContentsOfFile:filePath];
imgView.backgroundColor = [UIColor clearColor];
 imgView.animatedImage = [FLAnimatedImage animatedImageWithGIFData:imageData];

其它方法

一些第三方框架等

效果圖:


圖片來自網(wǎng)絡(luò),侵權(quán)立刪
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Gif圖片是非常常見的圖片格式,尤其是在聊天的過程中,Gif表情使用地很頻繁。但是iOS竟然沒有現(xiàn)成的支持加載和播...
    weicyNO_1閱讀 1,823評論 0 0
  • Gif圖片是非常常見的圖片格式,尤其是在聊天的過程中,Gif表情使用地很頻繁。但是iOS竟然沒有現(xiàn)成的支持加載和播...
    iOS_大菜鳥閱讀 3,082評論 1 5
  • 1.系統(tǒng)UIImageView 多張圖片組成動畫 /** * UIImageView 動畫 * Memor...
    zhengelababy閱讀 9,283評論 3 6
  • 原生的UIImageView是不支持gif格式圖片的,以下是我總結(jié)的三種方法,希望可以幫助到你。 一、用UIWeb...
    melody5閱讀 3,059評論 0 2
  • 1. 原生方法: UIWebView特點(diǎn):加載速度略長,性能更優(yōu),播放的gif動態(tài)圖更加流暢。 UIImagVie...
    奮斗的蝸牛閱讀 4,457評論 1 1

友情鏈接更多精彩內(nèi)容