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来加载(这个就不提供代码了)
网友评论