commit 39fa973a8026e896e2cd904ffc73a2a0849409c3
parent b70ce53b88472d4062a4fc110e546d7f9142d4ac
Author: OlegAba <mail@olegaba.com>
Date: Tue, 3 Jan 2023 23:37:14 -0500
Add KingFisher downsampler processor for large images
Changelog-Added: Downscale images if they are unreasonably large
Closes: #240
Diffstat:
1 file changed, 25 insertions(+), 0 deletions(-)
diff --git a/damus/Views/ProfilePicView.swift b/damus/Views/ProfilePicView.swift
@@ -55,6 +55,7 @@ struct InnerProfilePicView: View {
KFAnimatedImage(url)
.callbackQueue(.dispatch(.global(qos: .background)))
.processingQueue(.dispatch(.global(qos: .background)))
+ .appendProcessor(LargeImageProcessor())
.configure { view in
view.framePreloadCount = 1
}
@@ -104,6 +105,30 @@ struct ProfilePicView: View {
}
}
+struct LargeImageProcessor: ImageProcessor {
+
+ let identifier: String = "com.damus.largeimageprocessor"
+ let maxSize: Int = 1000000
+ let downsampleSize = CGSize(width: 200, height: 200)
+
+ func process(item: ImageProcessItem, options: KingfisherParsedOptionsInfo) -> KFCrossPlatformImage? {
+ let downsamplingImageProcessor = DownsamplingImageProcessor(size: downsampleSize)
+
+ switch item {
+ case .image(let image):
+ if image.cacheCost > maxSize {
+ return downsamplingImageProcessor.process(item: item, options: options)
+ }
+ return image
+ case .data(let data):
+ if data.count > maxSize {
+ return downsamplingImageProcessor.process(item: item, options: options)
+ }
+ return KFCrossPlatformImage(data: data)
+ }
+ }
+}
+
func get_profile_url(picture: String?, pubkey: String, profiles: Profiles) -> URL {
let pic = picture ?? profiles.lookup(id: pubkey)?.picture ?? robohash(pubkey)
if let url = URL(string: pic) {