- 1、导入头文件
import ImageIO
- 2、示例代码
class ViewController: UIViewController {
@IBOutlet weak var imgView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// 1、加载gif图片,并转成Data类型
guard let imgPath = Bundle.main.path(forResource: "dancer.gif", ofType: nil) else { return }
guard let imgData = NSData(contentsOfFile: imgPath) else { return }
// 2、从data中读取数据:将data转成CGImageSource对象
guard let imgSource = CGImageSourceCreateWithData(imgData, nil) else {
return
}
// 3、获取在CGImageSource中图片的个数
let imgCount = CGImageSourceGetCount(imgSource)
// 4、遍历所有图片
var imgs = [UIImage]()
var totalDuration: TimeInterval = 0
for i in 0..<imgCount {
// 4.1、取出图片
guard let cgimg = CGImageSourceCreateImageAtIndex(imgSource, i, nil) else { continue }
let img = UIImage(cgImage: cgimg)
if i == 0 { // 保证执行完一次gif后不消失
imgView.image = img
}
imgs.append(img)
// 4.2、取出持续的时间
guard let properties = CGImageSourceCopyPropertiesAtIndex(imgSource, i, nil) as? NSDictionary else { continue }
guard let dict = properties[kCGImagePropertyGIFDictionary] as? NSDictionary else { continue }
guard let duration = dict[kCGImagePropertyGIFDelayTime] as? NSNumber else { continue }
totalDuration += duration.doubleValue
}
// 5、设置imageView的属性
imgView.animationImages = imgs
imgView.animationDuration = totalDuration
imgView.animationRepeatCount = 1 // 执行一次,设置为0时无限执行
// 6、开始播放
imgView.startAnimating()
}
}
3、视图效果