美文网首页iOS开发笔录
利用 UIImageview播放gif动画

利用 UIImageview播放gif动画

作者: 转身一世琉璃白 | 来源:发表于2017-05-22 14:32 被阅读26次
    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:可设置动画执行的次数
    

    相关文章

      网友评论

        本文标题:利用 UIImageview播放gif动画

        本文链接:https://www.haomeiwen.com/subject/ovhgnttx.html