mirror of
https://github.com/neon443/StickerSlack.git
synced 2026-11-30 12:44:51 +00:00
increasing performance on gifview
This commit is contained in:
2 files changed
+25
-28
No files matched your search
@@ -13,15 +13,15 @@ class GifManager {
|
|||||||
//from clock-run, 12 frames one second
|
//from clock-run, 12 frames one second
|
||||||
static let defaultDuration: Double = 0.083333333333333329
|
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)
|
let (data, _) = try await URLSession.shared.data(from: url)
|
||||||
return gifFrom(data: data)
|
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") }
|
guard let source = CGImageSourceCreateWithData(data as NSData, nil) else { fatalError("couldnt create source") }
|
||||||
let frameCount = CGImageSourceGetCount(source)
|
let frameCount = CGImageSourceGetCount(source)
|
||||||
var result: [(frame: CGImage, showFor: Double)] = []
|
var result: [(frame: UIImage, showFor: Double)] = []
|
||||||
|
|
||||||
for i in 0..<frameCount {
|
for i in 0..<frameCount {
|
||||||
guard let cgImage = CGImageSourceCreateImageAtIndex(source, i, nil) else {
|
guard let cgImage = CGImageSourceCreateImageAtIndex(source, i, nil) else {
|
||||||
@@ -31,9 +31,9 @@ class GifManager {
|
|||||||
if let properties = CGImageSourceCopyPropertiesAtIndex(source, i, nil),
|
if let properties = CGImageSourceCopyPropertiesAtIndex(source, i, nil),
|
||||||
let gifInfo = (properties as NSDictionary)[kCGImagePropertyGIFDictionary as String] as? NSDictionary,
|
let gifInfo = (properties as NSDictionary)[kCGImagePropertyGIFDictionary as String] as? NSDictionary,
|
||||||
let frameDuration = (gifInfo[kCGImagePropertyGIFDelayTime as String] as? NSNumber) {
|
let frameDuration = (gifInfo[kCGImagePropertyGIFDelayTime as String] as? NSNumber) {
|
||||||
result.append((cgImage, frameDuration.doubleValue))
|
result.append((UIImage(cgImage: cgImage), frameDuration.doubleValue))
|
||||||
} else {
|
} else {
|
||||||
result.append((cgImage, defaultDuration))
|
result.append((UIImage(cgImage: cgImage), defaultDuration))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
|
|||||||
@@ -13,26 +13,18 @@ import WebKit
|
|||||||
struct GifView: View {
|
struct GifView: View {
|
||||||
@State var sticker: StickerProtocol?
|
@State var sticker: StickerProtocol?
|
||||||
@State var url: URL
|
@State var url: URL
|
||||||
@State var gif: [(frame: CGImage, showFor: Double)] = []
|
@State var gif: [(frame: UIImage, showFor: Double)] = []
|
||||||
@State var currentI: Int = 0
|
@State var currentI: Int = 0
|
||||||
@State var animate: Bool
|
@State var animate: Bool
|
||||||
|
|
||||||
@State var timer: Timer?
|
@State var timer: Timer?
|
||||||
|
// @State var timer: Task<Void, Never>?
|
||||||
|
|
||||||
func run() async {
|
@MainActor func run() async {
|
||||||
guard url.pathExtension == "gif" else {
|
guard url.pathExtension == "gif" else {
|
||||||
Task.detached {
|
if let data = try? await URLSession.shared.data(from: url).0,
|
||||||
if let data = try? await URLSession.shared.data(from: url).0,
|
let uiImage = UIImage(data: data) {
|
||||||
let uiImage = UIImage(data: data),
|
updateGif([(uiImage, 1)])
|
||||||
let cgImage = uiImage.cgImage {
|
|
||||||
if animate {
|
|
||||||
withAnimation(.spring(duration: 0.2)) {
|
|
||||||
self.gif = [(cgImage, 1)]
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
self.gif = [(cgImage, 1)]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -40,13 +32,7 @@ struct GifView: View {
|
|||||||
do {
|
do {
|
||||||
if gif.isEmpty {
|
if gif.isEmpty {
|
||||||
let frames = try await GifManager.gifFrom(url: url)
|
let frames = try await GifManager.gifFrom(url: url)
|
||||||
if animate {
|
updateGif(frames)
|
||||||
withAnimation(.spring(duration: 0.2)) {
|
|
||||||
self.gif = frames
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
self.gif = frames
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
print(error)
|
print(error)
|
||||||
@@ -69,6 +55,17 @@ struct GifView: View {
|
|||||||
RunLoop.main.add(timer!, forMode: .common)
|
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 {
|
var body: some View {
|
||||||
// /*@START_MENU_TOKEN@*//*@PLACEHOLDER=Hello, world!@*/Text("Hello, world!")/*@END_MENU_TOKEN@*/
|
// /*@START_MENU_TOKEN@*//*@PLACEHOLDER=Hello, world!@*/Text("Hello, world!")/*@END_MENU_TOKEN@*/
|
||||||
Group {
|
Group {
|
||||||
@@ -77,7 +74,7 @@ struct GifView: View {
|
|||||||
.modifier(fadeIn(enabled: animate))
|
.modifier(fadeIn(enabled: animate))
|
||||||
} else if url.pathExtension == "gif" {
|
} else if url.pathExtension == "gif" {
|
||||||
if currentI < gif.count {
|
if currentI < gif.count {
|
||||||
Image(uiImage: .init(cgImage: gif[currentI].frame))
|
Image(uiImage: gif[currentI].frame)
|
||||||
.resizable().scaledToFit()
|
.resizable().scaledToFit()
|
||||||
.onDisappear {
|
.onDisappear {
|
||||||
timer?.invalidate()
|
timer?.invalidate()
|
||||||
@@ -86,7 +83,7 @@ struct GifView: View {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if let frame = gif.first {
|
if let frame = gif.first {
|
||||||
Image(uiImage: .init(cgImage: frame.frame))
|
Image(uiImage: frame.frame)
|
||||||
.resizable().scaledToFit()
|
.resizable().scaledToFit()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||