Skip to content

Instantly share code, notes, and snippets.

View dmytro-anokhin's full-sized avatar
🔨

Dmytro Anokhin dmytro-anokhin

🔨
  • London
View GitHub Profile
for i in 0..<imageDecoder.frameCount {
let cgImage = imageDecoder.createFrameImage(at: i)
let duration = imageDecoder.frameDuration(at: i)
}
let cgImage = imageDecoder.createFrameImage(at: 0)
let imageDecoder = ImageDecoder()
var imageData = Data()
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
imageData.append(data)
imageDecoder.setData(imageData, allDataReceived: false)
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
let task = urlSession.dataTask(with: url) { data, _, _ in
guard let data = data else {
return
}
let imageDecoder = ImageDecoder()
imageDecoder.setData(data, allDataReceived: true)
guard let uiImage = imageDecoder.uiImage else {
return
let imageDecoder = ImageDecoder()
imageDecoder.setData(data, allDataReceived: true)
struct AnimatedImage: UIViewRepresentable {
let uiImage: UIImage
func makeUIView(context: UIViewRepresentableContext<AnimatedImage>) -> UIImageView {
let imageView = UIImageView(image: uiImage)
imageView.contentMode = .scaleAspectFit
imageView.startAnimating()
return imageView
var uiImage: UIImage? {
switch frameCount {
case 0:
return nil
case 1:
return staticUIImage
default:
return animatedUIImage
}
}
func isFrameComplete(at index: Int) -> Bool {
assert(frameCount > index)
// CGImageSourceGetStatusAtIndex() changes the return status value from kCGImageStatusIncomplete
// to kCGImageStatusComplete only if (index > 1 && index < frameCount() - 1). To get an accurate
// result for the last frame (or the single frame of the static image) use CGImageSourceGetStatus()
// instead for this frame.
if index == frameCount - 1 {
return CGImageSourceGetStatus(imageSource) == .statusComplete
}
func createFrameImage(at index: Int, subsamplingLevel: SubsamplingLevel = .default, decodingOptions: DecodingOptions = .default) -> CGImage? {
guard index < frameCount else {
return nil
}
let image: CGImage?
let options: CFDictionary
switch decodingOptions.mode {
case .asynchronous:
struct DecodingOptions {
enum Mode {
case synchronous
case asynchronous
}
static var `default`: DecodingOptions {