-
-
Save laevandus/8ad7d7bf05be0341754b4355fe7181e5 to your computer and use it in GitHub Desktop.
This file contains 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 ThumbnailSequence: AsyncSequence { | |
typealias AsyncIterator = Iterator | |
typealias Element = UIImage | |
var imageNames: [String] | |
let maxSize: CGSize | |
func makeAsyncIterator() -> Iterator { | |
return Iterator(imageNames: imageNames, maxSize: maxSize) | |
} | |
struct Iterator: AsyncIteratorProtocol { | |
typealias Element = UIImage | |
var imageNames: [String] | |
let maxSize: CGSize | |
mutating func next() async -> UIImage? { | |
guard !imageNames.isEmpty else { return nil } | |
guard let image = UIImage(named: imageNames.removeFirst()) else { return nil } | |
let ratio = image.size.height / maxSize.height // simplified scaling | |
return await image.byPreparingThumbnail(ofSize: CGSize(width: image.size.width / ratio, height: maxSize.height)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment