swift-05.02 Gif/WebP 图片的播放

Gif图片

import UIKit

import ImageIO

class ViewController: UIViewController {        

@IBOutlet weak var imageView: UIImageView!        

override func viewDidLoad() {        

super.viewDidLoad()                

// gif --> 一帧帧图片        

// imageView.image = UIImage(named: "demo.gif")   

 }        

override func touchesBegan(_ touches: Set, with event: UIEvent?) {

// 1.获取图片的路径

guard let filePath = Bundle.main.path(forResource: "demo.gif", ofType: nil) else  { return }

// 2.根据路径, 将gif图转成NSData类型

guard let imageData = NSData(contentsOfFile: filePath) else { return }

// 3.根据imageData创建CGImageSource

guard let imageSource = CGImageSourceCreateWithData(imageData, nil) else { return }

// 4.获取imageSource中的图片的个数

let frameCount = CGImageSourceGetCount(imageSource)

// 5.获取一张张的图片

var images = [UIImage]()

var duration : TimeInterval = 0

for i in 0..

// 5.1.获取图片

guard let cgImage = CGImageSourceCreateImageAtIndex(imageSource, i, nil) else {

continue

}

images.append(UIImage(cgImage: cgImage))

// 5.2.获取每一帧的时间

guard let imageDict = CGImageSourceCopyPropertiesAtIndex(imageSource, i, nil) as? [String : Any] else {

continue

}

guard let gifDict = imageDict[kCGImagePropertyGIFDictionary as String] as? [String : Any] else {

continue

}

guard let frameDuration = gifDict[kCGImagePropertyGIFDelayTime as String] as? NSNumber else {

continue

}

duration += frameDuration.doubleValue

}

// 播放图片

imageView.animationImages = images

imageView.animationDuration = duration

imageView.animationRepeatCount = 0

imageView.startAnimating()

}

}

Webp图片

pod 'Kingfisher'

pod 'YYWebImage'

pod 'YYImage/WebP'

imageView.autoPlayAnimatedImage = true

let url = URL(string: "https://file.qf.56.com/f/style/static/gift/m/v2/webp/paoche.webp")

imageView.yy_setImage(with: url, placeholder: nil)

if YYImageWebPAvailable() {

print("支持")

} else {

print("不支持")

}

你可能感兴趣的:(swift-05.02 Gif/WebP 图片的播放)