您的位置:首页 > 其它

加载一张gif动态图

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

第一种方法:

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

    

    webView = [[UIWebView alloc]init];

    webView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.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 mainBundle] pathForResource:@"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:name
ofType: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];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  加载gif图片