From 4d388eb4b0d64a2aadfd39f3660f951c2e3881e9 Mon Sep 17 00:00:00 2001 From: neon443 <69979447+neon443@users.noreply.github.com> Date: Mon, 4 May 2026 21:21:19 +0100 Subject: [PATCH] increasing performance on gifview --- StickerSlack/Emoji/GifManager.swift | 10 +++---- StickerSlack/Emoji/GifView.swift | 43 ++++++++++++++--------------- 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/StickerSlack/Emoji/GifManager.swift b/StickerSlack/Emoji/GifManager.swift index a00f90f..a742501 100644 --- a/StickerSlack/Emoji/GifManager.swift +++ b/StickerSlack/Emoji/GifManager.swift @@ -13,15 +13,15 @@ class GifManager { //from clock-run, 12 frames one second static let defaultDuration: Double = 0.083333333333333329 - static func gifFrom(url: URL) async throws -> [(frame: CGImage, showFor: Double)] { + static func gifFrom(url: URL) async throws -> [(frame: UIImage, showFor: Double)] { let (data, _) = try await URLSession.shared.data(from: url) return gifFrom(data: data) } - static func gifFrom(data: Data) -> [(frame: CGImage, showFor: Double)] { + static func gifFrom(data: Data) -> [(frame: UIImage, showFor: Double)] { guard let source = CGImageSourceCreateWithData(data as NSData, nil) else { fatalError("couldnt create source") } let frameCount = CGImageSourceGetCount(source) - var result: [(frame: CGImage, showFor: Double)] = [] + var result: [(frame: UIImage, showFor: Double)] = [] for i in 0..? - func run() async { + @MainActor func run() async { guard url.pathExtension == "gif" else { - Task.detached { - if let data = try? await URLSession.shared.data(from: url).0, - let uiImage = UIImage(data: data), - let cgImage = uiImage.cgImage { - if animate { - withAnimation(.spring(duration: 0.2)) { - self.gif = [(cgImage, 1)] - } - } else { - self.gif = [(cgImage, 1)] - } - } + if let data = try? await URLSession.shared.data(from: url).0, + let uiImage = UIImage(data: data) { + updateGif([(uiImage, 1)]) } return } @@ -40,13 +32,7 @@ struct GifView: View { do { if gif.isEmpty { let frames = try await GifManager.gifFrom(url: url) - if animate { - withAnimation(.spring(duration: 0.2)) { - self.gif = frames - } - } else { - self.gif = frames - } + updateGif(frames) } } catch { print(error) @@ -69,6 +55,17 @@ struct GifView: View { RunLoop.main.add(timer!, forMode: .common) } + @MainActor + func updateGif(_ frames: [(UIImage, Double)]) { + if animate { + withAnimation(.spring(duration: 0.2)) { + self.gif = frames + } + } else { + self.gif = frames + } + } + var body: some View { // /*@START_MENU_TOKEN@*//*@PLACEHOLDER=Hello, world!@*/Text("Hello, world!")/*@END_MENU_TOKEN@*/ Group { @@ -77,7 +74,7 @@ struct GifView: View { .modifier(fadeIn(enabled: animate)) } else if url.pathExtension == "gif" { if currentI < gif.count { - Image(uiImage: .init(cgImage: gif[currentI].frame)) + Image(uiImage: gif[currentI].frame) .resizable().scaledToFit() .onDisappear { timer?.invalidate() @@ -86,7 +83,7 @@ struct GifView: View { } } else { if let frame = gif.first { - Image(uiImage: .init(cgImage: frame.frame)) + Image(uiImage: frame.frame) .resizable().scaledToFit() } }