加载一张gif动态图

来源:互联网 发布:网卡的mac地址 编辑:程序博客网 时间:2024/06/10 14:10

不知道大家有没有碰到过加载一张gif图的问题 这里我整理了两个方法第一个是通过html  加载到一个Web上   另一个是通过SDWebImage来实现的  其实两个原理都是差不多的  使用第三方可能比较简单  但是大家可以可以自己写一下,话不多说上代码:


第一种方法:

  [super.navigationController setNavigationBarHidden:YES animated:TRUE];

    

    webView = [[UIWebView alloc]init];

    webView.frame = CGRectMake(00self.view.bounds.size.widthself.view.bounds.size.height);


    // 设定位置和大小

    NSString *html = [NSString stringWithFormat:@"<html><head><style>body{margin:0px;padding:0px}</style></head><body><img border=\"0\" src = \"searchwait.gif\" width=\"%f\"height=\"%f\" style=\"position: absolute; z-index: -1;margin:0px;padding:0px\"><body></head></html>",self.view.bounds.size.width,self.view.bounds.size.height];

    NSString *path = [[NSBundle mainBundlepathForResource:@"searchwait"ofType:@"gif"];

    NSURL *baseURL = [NSURL fileURLWithPath:path];

    [webView loadHTMLString:html baseURL:baseURL];

    webView.userInteractionEnabled = NO;

    webView.backgroundColor  = wc_white;

    webView.contentMode      = UIViewContentModeScaleAspectFill;

    webView.clipsToBounds    = YES;

    

    [self.view addSubview:webView];


第二种方法:

- (void)initLoadingImageView

{

    NSString  *name =@"loading.gif";

    NSString  *filePath = [[NSBundlebundleWithPath:[[NSBundlemainBundle]bundlePath]]pathForResource:nameofType:nil];

    NSData  *imageData = [NSDatadataWithContentsOfFile:filePath];

    

    if (!self.loadingImageView) {

        self.loadingImageView = [[UIImageView alloc]init];

    }

    //    imgView.image = [UIImage animatedImageWithAnimatedGIFData:imageData];

    self.loadingImageView.backgroundColor = [UIColor clearColor];

    self.loadingImageView.image = [UIImage sd_animatedGIFWithData:imageData];

    self.loadingImageView.frame = CGRectMake(0,0,100,250/2.0);

    [self.view addSubview:self.loadingImageView];

    [self.view bringSubviewToFront:self.loadingImageView];

}





0 0
原创粉丝点击