This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for i in 0..<imageDecoder.frameCount { | |
| let cgImage = imageDecoder.createFrameImage(at: i) | |
| let duration = imageDecoder.frameDuration(at: i) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let cgImage = imageDecoder.createFrameImage(at: 0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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?) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let imageDecoder = ImageDecoder() | |
| imageDecoder.setData(data, allDataReceived: true) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct AnimatedImage: UIViewRepresentable { | |
| let uiImage: UIImage | |
| func makeUIView(context: UIViewRepresentableContext<AnimatedImage>) -> UIImageView { | |
| let imageView = UIImageView(image: uiImage) | |
| imageView.contentMode = .scaleAspectFit | |
| imageView.startAnimating() | |
| return imageView |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var uiImage: UIImage? { | |
| switch frameCount { | |
| case 0: | |
| return nil | |
| case 1: | |
| return staticUIImage | |
| default: | |
| return animatedUIImage | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct DecodingOptions { | |
| enum Mode { | |
| case synchronous | |
| case asynchronous | |
| } | |
| static var `default`: DecodingOptions { |