import UIKit
extension UIImageView {
// Gif 파일 로드
static func fromGif(frame: CGRect, resourceName: String) -> UIImageView? {
guard let path = Bundle.main.path(forResource: resourceName, ofType: "gif") else {
print("Gif does not exist at that path")
return nil
}
let url = URL(fileURLWithPath: path)
guard let gifData = try? Data(contentsOf: url),
let source = CGImageSourceCreateWithData(gifData as CFData, nil) else { return nil }
var images = [UIImage]()
let imageCount = CGImageSourceGetCount(source)
for i in 0 ..< imageCount {
if let image = CGImageSourceCreateImageAtIndex(source, i, nil) {
images.append(UIImage(cgImage: image))
}
}
let gifImageView = UIImageView(frame: frame)
gifImageView.animationImages = images
return gifImageView
}
}
[사용방법]
guard let popupView = UIImageView.fromGif(frame: CGRect.init(x: 0, y: 0, width: 100, height: 100), resourceName: "ico_loading_dot") else {
return
}
view.addSubview(popupView)
'Swift > Extention' 카테고리의 다른 글
[SWIFT]2진수 to 8Bit String 변환 (0) | 2023.10.10 |
---|---|
[SWIFT]Data Extention (0) | 2023.07.03 |
[SWIFT]UIFont Extension (0) | 2023.06.09 |
[SWIFT]Int Extension (String, PriceString, TimeString) (0) | 2023.06.09 |
[SWIFT]Bundle Extension (앱 정보 가져오기) (0) | 2023.05.16 |