Skip to content

Instantly share code, notes, and snippets.

@brownsoo
Created August 10, 2024 17:57
Show Gist options
  • Save brownsoo/64462bd0e1519744f822a19d80b833ab to your computer and use it in GitHub Desktop.
Save brownsoo/64462bd0e1519744f822a19d80b833ab to your computer and use it in GitHub Desktop.
gif 파일 디스플레이
//
// UIImageView+.swift
// HEExample
//
// Created by hyonsoo on 8/11/24.
//
import UIKit
import ImageIO
extension UIImageView {
func loadGif(url: URL) {
DispatchQueue.global().async {
guard let imageData = try? Data(contentsOf: url),
let source = CGImageSourceCreateWithData(imageData as CFData, nil) else {
return
}
let meta = imageData.he.metadataForImageData()
var delay: Double = 0.1
if let gifInfo = meta["{GIF}"] as? [String: Any],
let delayTime = gifInfo["DelayTime"] as? Double {
delay = delayTime
debugPrint(gifInfo)
}
var images = [UIImage]()
let count = CGImageSourceGetCount(source)
for i in 0..<count {
if let cgImage = CGImageSourceCreateImageAtIndex(source, i, nil) {
images.append(UIImage(cgImage: cgImage))
}
}
DispatchQueue.main.async {
self.animationImages = images
self.animationDuration = Double(count) * delay
self.startAnimating()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment