Skip to content

Commit

Permalink
attempt to build a means of stopping excessively large animated image…
Browse files Browse the repository at this point in the history
…s from blocking main thread
  • Loading branch information
simonmcl committed Jan 24, 2024
1 parent 854cd9e commit 5a357a7
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions Sources/KukaiCoreSwift/Services/MediaProxyService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public class MediaProxyService: NSObject {
- parameter downSampleSize: Supply the dimensions you wish the image to be resized to fit
- parameter completion: returns when operation finished, if successful it will return the downloaded image's CGSize
*/
public static func load(url: URL?, to imageView: UIImageView, withCacheType cacheType: CacheType, fallback: UIImage, downSampleSize: CGSize? = nil, completion: ((CGSize?) -> Void)? = nil) {
public static func load(url: URL?, to imageView: UIImageView, withCacheType cacheType: CacheType, fallback: UIImage, downSampleSize: CGSize? = nil, maxAnimatedImageSize: UInt? = nil, completion: ((CGSize?) -> Void)? = nil) {
guard let url = url else {
imageView.image = fallback
if let comp = completion { comp(nil) }
Expand All @@ -367,15 +367,20 @@ public class MediaProxyService: NSObject {

context[.imageCache] = imageCache(forType: cacheType)


imageView.sd_imageIndicator = (isDarkMode) ? SDWebImageActivityIndicator.white : SDWebImageActivityIndicator.gray
imageView.sd_setImage(with: url, placeholderImage: nil, context: context) { _, _, _ in
imageView.sd_setImage(with: url, placeholderImage: nil, options: [.avoidAutoSetImage], context: context) { _, _, _ in

} completed: { image, error, _, _ in
if let _ = error {
imageView.image = fallback
}

if image?.sd_isAnimated == true, let animatedImage = image as? SDAnimatedImage, let maxMemory = maxAnimatedImageSize, (image?.sd_memoryCost ?? 0) > maxMemory {
imageView.image = animatedImage.animatedImageFrame(at: 0)
} else {
imageView.image = image
}

completion?(image?.size)
}
}
Expand Down

0 comments on commit 5a357a7

Please sign in to comment.