欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

用 UIImageview 播放动画 gif

最编程 2024-03-17 15:49:41
...

利用 UIImageview播放gif动画

NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"gif_icon_w" withExtension:@"gif"];
//将GIF图片转换成对应的图片源   
CGImageSourceRef gifSource = CGImageSourceCreateWithURL((CFURLRef)fileUrl, NULL);
//获取其中图片源个数,即由多少帧图片组成
size_t frameCount=CGImageSourceGetCount(gifSource);
//定义数组存储拆分出来的图片
NSMutableArray* frames=[[NSMutableArray alloc] init];    
for (size_t i=0;i < frameCount; i++)
{
   CGImageRef imageRef=CGImageSourceCreateImageAtIndex(gifSource , i , NULL);//从GIF图片中取出源图片
  UIImage* imageName=[UIImage imageWithCGImage:imageRef];//将图片源转换成UIimageView能使用的图片源
        [frames addObject:imageName];//将图片加入数组中
        CGImageRelease(imageRef);
} 
self.gifImgView.animationImages=frames;//将图片数组加入UIImageView动画数组中
self.gifImgView.animationDuration=0.9;//每次动画时长
[self.gifImgView startAnimating];//开启动画,UIImageView默认播放次数为无限次,animationRepeatCount:可设置动画执行的次数
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者