Add KingFisher downsampler processor for large images
Changelog-Added: Downscale images if they are unreasonably large Closes: #240
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user