fix gif caching
This commit is contained in:
1 parent
71b1d0a21d
commit
395cbe5e13
19 files changed
+728
-475
No files matched your search
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// File.swift
|
||||
//
|
||||
//
|
||||
// Created by Igor Ferreira on 05/04/2022.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import ImageIO
|
||||
|
||||
extension CFString {
|
||||
var asKey: UnsafeMutableRawPointer {
|
||||
return Unmanaged.passUnretained(self).toOpaque()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
//
|
||||
// File.swift
|
||||
//
|
||||
//
|
||||
// Created by Igor Ferreira on 05/04/2022.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import ImageIO
|
||||
|
||||
extension CGImageSource {
|
||||
func intervalAtIndex(_ index: Int) -> TimeInterval? {
|
||||
guard let properties = CGImageSourceCopyPropertiesAtIndex(self, index, nil) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
guard let gifProperties = CFDictionaryGetValue(properties, kCGImagePropertyGIFDictionary.asKey) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let dictionary = unsafeBitCast(gifProperties, to: CFDictionary.self)
|
||||
|
||||
let pointer: UnsafeRawPointer
|
||||
if let delay = CFDictionaryGetValue(dictionary, kCGImagePropertyGIFDelayTime.asKey) {
|
||||
pointer = delay
|
||||
} else if let unclampedDelay = CFDictionaryGetValue(dictionary, kCGImagePropertyGIFUnclampedDelayTime.asKey) {
|
||||
pointer = unclampedDelay
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let interval = unsafeBitCast(pointer, to: AnyObject.self).doubleValue ?? 0.0
|
||||
if interval > 0.0 {
|
||||
return interval
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// File.swift
|
||||
//
|
||||
//
|
||||
// Created by Igor Ferreira on 05/04/2022.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import ImageIO
|
||||
|
||||
extension Data {
|
||||
func imageAsyncSequence() throws -> CGImageSourceFrameSequence {
|
||||
return try CGImageSourceFrameSequence(data: self)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// File.swift
|
||||
//
|
||||
//
|
||||
// Created by Igor Ferreira on 05/04/2022.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct GIFImageEnvironment: EnvironmentKey {
|
||||
static var defaultValue: ImageLoader = {
|
||||
return ImageLoader()
|
||||
}()
|
||||
}
|
||||
|
||||
extension EnvironmentValues {
|
||||
var imageLoader: ImageLoader {
|
||||
get { self[GIFImageEnvironment.self] }
|
||||
set { self[GIFImageEnvironment.self] = newValue }
|
||||
}
|
||||
}
|
||||
|
||||
extension View {
|
||||
@ViewBuilder
|
||||
func with(imageLoader: ImageLoader) -> some View {
|
||||
environment(\.imageLoader, imageLoader)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user