iOS 加载gif 的两种方式

18 篇文章 0 订阅
11 篇文章 0 订阅

简单记录一下,排除第三方加载gif

1. 使用webview ,可以根据需要将webview放到一些控件上

如下:

-(void)loadGif{
    //1.找到gif文件路径 ,SDKPlatform.bundle/loading1 是所在SDKPlatform.bundle中
    
    NSString *dataPath = [[NSBundle mainBundle]pathForResource:@"SDKPlatform.bundle/loading1" ofType:@"gif"];
    //转化成data
    NSData *gif = [NSData dataWithContentsOfFile:dataPath];
    
    UIWebView *webViewBG = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0,WDScale(250), WDScale(160))];
    //加载data的形式
    [webViewBG loadData:gif MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];

    [webViewBG setScalesPageToFit:YES];//自动适应当前frame大小
    [self.view addSubview:webViewBG];
}

或者如下

-(void)loadGif{

    UIWebView *webViewBG = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0,WDScale(250), WDScale(160))];

//根据路径转成NSURL

    NSURL *url = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"SDKPlatform.bundle/loading1" ofType:@"gif"]];


    [webViewBG loadRequest:[NSURLRequest requestWithURL:url]];
    [webViewBG setScalesPageToFit:YES];//自动适应当前frame大小

    [self.view addSubview:webViewBG];
}

2.使用拆分gif图 ,imageView来动画加载图片

- (void)createGIF {
    //1.找到gif文件路径
    NSString *dataPath = [[NSBundle mainBundle]pathForResource:@"SDKPlatform.bundle/loading1" ofType:@"gif"];

    //2.获取gif文件数据
    
    CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:dataPath], NULL);
    
    //3.获取gif文件中图片的个数
    size_t count = CGImageSourceGetCount(source);
    
    //5.定义一个可变数组存放所有图片
    NSMutableArray *imageArray = [[NSMutableArray alloc] init];

    //遍历gif
    
    for (size_t i=0; i<count; i++) {
        
        CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
        UIImage* imageName=[UIImage imageWithCGImage:image];
        [imageArray addObject:imageName];//存image
        
        CGImageRelease(image);
  
    }
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,WDScale(250), WDScale(160))];
    [self.view addSubview:imageView];

    imageView.animationImages=imageArray;//将图片数组加入UIImageView动画数组中
    imageView.animationDuration=2;//每次动画时长
    [imageView startAnimating];
    
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值