美文网首页
iOS 播放Gif动画

iOS 播放Gif动画

作者: 霍达_69e7 | 来源:发表于2020-09-27 16:29 被阅读0次

1,使用ImageIO框架中的方法将图片数据解析成图片数组,使用UIImageView的帧动画接口进行播放

funcplayGIFOnImageView(name:String,imageView:UIImageView) {

        guardletpath =Bundle.main.path(forResource: name, ofType:"gif")else{

            return

        }

        leturl =URL.init(string: path)

        let source = CGImageSourceCreateWithURL(url! as CFURL, nil)

        letcount =CGImageSourceGetCount(source!)

        varimageArray =Array()

        varimagesWidth =Array()

        varimagesHeight =Array()

        vartime =Int()

        forindexin0..<count {

            letimage =CGImageSourceCreateImageAtIndex(source!, index,nil)

            imageArray.append(UIImage(cgImage: image!))

            let info = CGImageSourceCopyPropertiesAtIndex(source!, index, nil) as! Dictionary<String,AnyObject>

            letwidth =Int(truncating: info[kCGImagePropertyPixelWidthasString]as!NSNumber)

            letheight =Int(truncating: info[kCGImagePropertyPixelHeightasString]as!NSNumber)

            imagesWidth.append(width)

            imagesHeight.append(height)

            let timeDic = info[kCGImagePropertyGIFDelayTime as String] as! Dictionary<String,AnyObject>

            time+= Int(truncating: timeDic[kCGImagePropertyGIFDelayTime as String] as! NSNumber)

        }

        imageView.frame=  CGRect(x:0, y:100, width: imagesWidth[0], height: imagesHeight[0])

        imageView.animationImages=  imageArray

        imageView.animationDuration = TimeInterval(time)

        imageView.animationRepeatCount=  0

        imageView.startAnimating()

    }

2.使用WKWebview来加载(这个就不提供代码了)

相关文章

网友评论

      本文标题:iOS 播放Gif动画

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