-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use image cached provider to reuse asset when creating RootAnimationL…
…ayer (#1590)
- Loading branch information
1 parent
b7725f5
commit c4f028a
Showing
3 changed files
with
59 additions
and
4 deletions.
There are no files selected for viewing
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
47 changes: 47 additions & 0 deletions
47
Sources/Private/MainThread/LayerContainers/Utility/CachedImageProvider.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Created by Jianjun Wu on 2022/5/12. | ||
// Copyright © 2022 Airbnb Inc. All rights reserved. | ||
|
||
import CoreGraphics | ||
import Foundation | ||
|
||
// MARK: - CachedImageProvider | ||
|
||
private final class CachedImageProvider: AnimationImageProvider { | ||
|
||
// MARK: Lifecycle | ||
|
||
/// Initializes an image provider with an image provider | ||
/// | ||
/// - Parameter imageProvider: The provider to load image from asset | ||
/// | ||
public init(imageProvider: AnimationImageProvider) { | ||
self.imageProvider = imageProvider | ||
} | ||
|
||
// MARK: Public | ||
|
||
public func imageForAsset(asset: ImageAsset) -> CGImage? { | ||
if let image = imageCache.object(forKey: asset.id as NSString) { | ||
return image | ||
} | ||
if let image = imageProvider.imageForAsset(asset: asset) { | ||
imageCache.setObject(image, forKey: asset.id as NSString) | ||
return image | ||
} | ||
return nil | ||
} | ||
|
||
// MARK: Internal | ||
|
||
let imageCache: NSCache<NSString, CGImage> = .init() | ||
let imageProvider: AnimationImageProvider | ||
} | ||
|
||
extension AnimationImageProvider { | ||
/// Create a cache enabled image provider which will reuse the asset image with the same asset id | ||
/// It wraps the current provider as image loader, and uses `NSCache` to cache the images for resue. | ||
/// The cache will be reset when the `animation` is reset. | ||
var cachedImageProvider: AnimationImageProvider { | ||
CachedImageProvider(imageProvider: self) | ||
} | ||
} |
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