Created
August 4, 2021 15:46
-
-
Save varun04/94b9754c44d3b4d70602b5f3a6fca4e6 to your computer and use it in GitHub Desktop.
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
| // | |
| // DBImageDecoder+UIKit.swift | |
| // Test | |
| // | |
| // Created by Varun Tomar on 04/08/21. | |
| // | |
| import UIKit | |
| extension DBImageDecoder { | |
| var uiImage: UIImage? { | |
| switch frameCount { | |
| case 0: | |
| return nil | |
| case 1: | |
| return staticUIImage | |
| default: | |
| return animatedUIImage | |
| } | |
| } | |
| var animatedUIImage: UIImage? { | |
| guard frameCount > 1 else { | |
| return nil | |
| } | |
| var duration: TimeInterval = 0.0 | |
| var images: [UIImage] = [] | |
| for i in 0..<frameCount { | |
| guard let image = createFrameUIImage(at: i) else { | |
| continue | |
| } | |
| images.append(image) | |
| duration += frameDuration(at: i) ?? 0.0 | |
| } | |
| return UIImage.animatedImage(with: images, duration: duration) | |
| } | |
| // If we need to have only static image, no matter has single or multiple frames. | |
| var staticUIImage: UIImage? { | |
| frameCount > 0 ? createFrameUIImage(at: 0) : nil | |
| } | |
| private func createFrameUIImage(at index: Int, downsamplingLevel: DownSamplingLevel = .default, decodingOptions: DecodingOptions = .default) -> UIImage? { | |
| guard let cgImage = createFrameImage(at: index, downsamplingLevel: downsamplingLevel, decodingOptions: decodingOptions) else { | |
| return nil | |
| } | |
| return UIImage(cgImage: cgImage) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment