From 8628deb1e4e70793347d805ad1f67d9bc459e32c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Metin=20G=C3=BCler?= Date: Wed, 11 Jan 2023 16:42:59 +0100 Subject: [PATCH] do image processing works in background threads this prevents fps drops by removing work from main thread and should improve performance with multiple gifs on screen --- Sources/SwiftUIGIF/SwiftUIGIF.swift | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Sources/SwiftUIGIF/SwiftUIGIF.swift b/Sources/SwiftUIGIF/SwiftUIGIF.swift index ce33c8e..c799cfc 100644 --- a/Sources/SwiftUIGIF/SwiftUIGIF.swift +++ b/Sources/SwiftUIGIF/SwiftUIGIF.swift @@ -63,11 +63,15 @@ public class UIGIFImage: UIView { } func updateGIF(data: Data) { - imageView.image = UIImage.gifImage(data: data) + Task { + imageView.image = await UIImage.gifImage(data: data) + } } func updateGIF(name: String) { - imageView.image = UIImage.gifImage(name: name) + Task { + imageView.image = await UIImage.gifImage(name: name) + } } private func initView() { @@ -76,7 +80,7 @@ public class UIGIFImage: UIView { } public extension UIImage { - class func gifImage(data: Data) -> UIImage? { + class func gifImage(data: Data) async -> UIImage? { guard let source = CGImageSourceCreateWithData(data as CFData, nil) else { return nil @@ -107,13 +111,13 @@ public extension UIImage { duration: Double(duration) / 1000.0) } - class func gifImage(name: String) -> UIImage? { + class func gifImage(name: String) async -> UIImage? { guard let url = Bundle.main.url(forResource: name, withExtension: "gif"), let data = try? Data(contentsOf: url) else { return nil } - return gifImage(data: data) + return await gifImage(data: data) } }