转自51CTO林家男孩 http://bj007.blog.51cto.com/1701577/533633
iPhone SDK提供了多种动画手段,UIView、UIImageView和CALayer都支持动画。但如何处理常见的gif动画呢?UIWebView提供了答案,代码如下:
// 设定位置和大小
CGRect frame = CGRectMake(50,50,0,0);
frame.size = [UIImage imageNamed:@"anim.gif"].size;
CGRect frame = CGRectMake(50,50,0,0);
frame.size = [UIImage imageNamed:@"anim.gif"].size;
// 读取gif图片数据
NSData *gif = [NSData dataWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:@"anim" ofType:@"gif"]];
NSData *gif = [NSData dataWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:@"anim" ofType:@"gif"]];
// view生成
UIWebView *view = [[UIWebView alloc] initWithFrame:frame];
[view loadData:gif MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
UIWebView *view = [[UIWebView alloc] initWithFrame:frame];
[view loadData:gif MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
webview加载的gif会保持原有的桢速。这个webview可以设置透明: [webView setBackgroundColor:[UIColor clearColor]]; [webView setOpaque:NO];这样就不会出现白的底色,当然,前提是gif的每个桢图也是背景透明的。