gif图片一定不要放在项的Images.xcassets中,放在项目其它地方就可以
NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"1" withExtension:@"gif"];//加载GIF图片
CGImageSourceRef gifSource = CGImageSourceCreateWithURL((CFURLRef)fileUrl, NULL);//将GIF图片转换成对应的图片源
size_t frameCout=CGImageSourceGetCount(gifSource);//获取其中图片源个数,即由多少帧图片组成
NSMutableArray* frames=[[NSMutableArray alloc] init];//定义数组存储拆分出来的图片
for (size_t i=0; i<frameCout;i++{
CGImageRef imageRef=CGImageSourceCreateImageAtIndex(gifSource, i, NULL);//从GIF图片中取出源图片
UIImage* imageName=[UIImage imageWithCGImage:imageRef];//将图片源转换成UIimageView能使用的图片源
[frames addObject:imageName];//将图片加入数组中
CGImageRelease(imageRef);
}
CFRelease(gifSource);
UIImageView* imageview=[[UIImageView alloc] initWithFrame:CGRectMake(20, 64, 40, 40)];
imageview.animationImages=frames;//将图片数组加入UIImageView动画数组中
imageview.animationDuration=3;//每次动画时长
[imageview startAnimating];//开启动画,此处没有调用播放次数接口,UIImageView默认播放次数为无限次,故这里不做处理
网友评论