Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit 89d4193

Browse files
nguyenhuyAdlai Holler
authored andcommitted
Open source pi_imageNamed methods (#2859)
1 parent 23c81b1 commit 89d4193

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

AsyncDisplayKit/UIImage+ASConvenience.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,27 @@ NS_ASSUME_NONNULL_BEGIN
7070
roundedCorners:(UIRectCorner)roundedCorners
7171
scale:(CGFloat)scale AS_WARN_UNUSED_RESULT;
7272

73+
/**
74+
* A version of imageNamed that caches results because loading an image is expensive.
75+
* Calling with the same name value will usually return the same object. A UIImage,
76+
* after creation, is immutable and thread-safe so it's fine to share these objects across multiple threads.
77+
*
78+
* @param imageName The name of the image to load
79+
* @return The loaded image or nil
80+
*/
81+
+ (UIImage *)as_imageNamed:(NSString *)imageName;
82+
83+
/**
84+
* A version of imageNamed that caches results because loading an image is expensive.
85+
* Calling with the same name value will usually return the same object. A UIImage,
86+
* after creation, is immutable and thread-safe so it's fine to share these objects across multiple threads.
87+
*
88+
* @param imageName The name of the image to load
89+
* @param traitCollection The traits associated with the intended environment for the image.
90+
* @return The loaded image or nil
91+
*/
92+
+ (UIImage *)as_imageNamed:(NSString *)imageName compatibleWithTraitCollection:(nullable UITraitCollection *)traitCollection;
93+
7394
@end
7495

7596
NS_ASSUME_NONNULL_END

AsyncDisplayKit/UIImage+ASConvenience.m

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,47 @@ + (UIImage *)as_resizableRoundedImageWithCornerRadius:(CGFloat)cornerRadius
122122
return result;
123123
}
124124

125+
#pragma mark - as_imageNamed
126+
127+
UIImage *cachedImageNamed(NSString *imageName, UITraitCollection *traitCollection)
128+
{
129+
static NSCache *imageCache = nil;
130+
static dispatch_once_t onceToken;
131+
dispatch_once(&onceToken, ^{
132+
// Because NSCache responds to memory warnings, we do not need an explicit limit.
133+
// all of these objects contain compressed image data and are relatively small
134+
// compared to the backing stores of text and image views.
135+
imageCache = [[NSCache alloc] init];
136+
});
137+
138+
UIImage *image = nil;
139+
if ([imageName length] > 0) {
140+
NSString *imageKey = imageName;
141+
if (traitCollection) {
142+
char imageKeyBuffer[256];
143+
snprintf(imageKeyBuffer, sizeof(imageKeyBuffer), "%s|%ld|%ld", imageName.UTF8String, (long)traitCollection.horizontalSizeClass, (long)traitCollection.verticalSizeClass);
144+
imageKey = [NSString stringWithUTF8String:imageKeyBuffer];
145+
}
146+
147+
image = [imageCache objectForKey:imageKey];
148+
if (!image) {
149+
image = [UIImage imageNamed:imageName inBundle:nil compatibleWithTraitCollection:traitCollection];
150+
if (image) {
151+
[imageCache setObject:image forKey:imageKey];
152+
}
153+
}
154+
}
155+
return image;
156+
}
157+
158+
+ (UIImage *)as_imageNamed:(NSString *)imageName
159+
{
160+
return cachedImageNamed(imageName, nil);
161+
}
162+
163+
+ (UIImage *)as_imageNamed:(NSString *)imageName compatibleWithTraitCollection:(UITraitCollection *)traitCollection
164+
{
165+
return cachedImageNamed(imageName, traitCollection);
166+
}
167+
125168
@end

0 commit comments

Comments
 (0)