iOS 播放GIf图, 动态效果

一.如果你集成了SDWebImage , 有一个很简单的方法

//导入sdwebImage的某个头文件

#import "UIImage+GIF.h"

    _bubble1.backgroundColor = [UIColor clearColor];

    NSString *str = [[NSBundle mainBundle]pathForResource:@"bubble" ofType:@"gif"];

    NSData *data = [NSData dataWithContentsOfFile:str];

    _bubble1.image = [UIImage sd_animatedGIFWithData:data];

*注意 : _bubble1 是自定义的一个全局的ImageView , 自己顶一下

二.使用webView  播放Gif图片

    // 设定位置和大小
    CGRect frame = CGRectMake(50,50,0,0);
    frame.size = [UIImage imageNamed:@"guzhang.gif"].size;
    // 读取gif图片数据
    NSData *gif = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"guzhang" ofType:@"gif"]];
    // view生成
    UIWebView *webView = [[UIWebView alloc] initWithFrame:frame];
    webView.userInteractionEnabled = NO;//用户不可交互
    [webView loadData:gif MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
    [self.view addSubview:webView];
    [webView release];

两个方法都可以用, 只是在内存上有区别

猜你喜欢

转载自blog.csdn.net/Keep_Moving31038/article/details/81388631