闲话少说直接上代码
class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// 1.加载Gif图片, 并转成Data类型
guard let path = Bundle.main.path(forResource: "test.gif", ofType: nil) else { return }
guard let data = NSData(contentsOfFile: path) else { return }
// 2.从data中读取数据: 将data转成CGImageSource对象
guard let imageSource = CGImageSourceCreateWithData(data, nil) else { return }
let imageCount = CGImageSourceGetCount(imageSource)
// 3.遍历所有的图片
var images = [UIImage]()
var totalDuration : TimeInterval = 0
for i in 0..<imageCount {
// 3.1.取出每一帧图片
guard let cgImage = CGImageSourceCreateImageAtIndex(imageSource, i, nil) else { continue }
let image = UIImage(cgImage: cgImage)
if i == 0 {
imageView.image = image
}
images.append(image)
// 3.2.取出持续的时间
guard let properties = CGImageSourceCopyPropertiesAtIndex(imageSource, i, nil) as? NSDictionary else { continue }
guard let gifDict = properties[kCGImagePropertyGIFDictionary] as? NSDictionary else { continue }
guard let frameDuration = gifDict[kCGImagePropertyGIFDelayTime] as? NSNumber else { continue }
totalDuration += frameDuration.doubleValue
}
// 4.设置imageView的属性
imageView.animationImages = images
imageView.animationDuration = totalDuration
imageView.animationRepeatCount = 0 //0代表不限重复次数(无限重复)
// 5.开始播放
imageView.startAnimating()
}
}
网友评论