Skip to content

Commit 1ec6922

Browse files
authored
[UIImage+ASConvenience] Reorder header and add more documentation. (facebookarchive#3004)
Order the methods so the most valuable one, as_imageNamed, is more immediately noticeable. Also added a block comment describing its purpose and usage suggestions. Separated @implementation for fairly unrelated methods by categories.
1 parent 6d9f12c commit 1ec6922

File tree

2 files changed

+95
-72
lines changed

2 files changed

+95
-72
lines changed

AsyncDisplayKit/UIImage+ASConvenience.h

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,52 @@
1515

1616
NS_ASSUME_NONNULL_BEGIN
1717

18-
// High-performance flat-colored, rounded-corner resizable images
19-
//
20-
// For "Baked-in Opaque" corners, set cornerColor equal to the color behind the rounded image object, i.e. the background color.
21-
// For "Baked-in Alpha" corners, set cornerColor = [UIColor clearColor]
22-
//
23-
// See http://asyncdisplaykit.org/docs/corner-rounding.html for an explanation.
18+
/**
19+
* Dramatically faster version of +[UIImage imageNamed:]. Although it is believed that imageNamed:
20+
* has a cache and is fast, it actually performs expensive asset catalog lookups and is often a
21+
* performance bottleneck (verified on iOS 7 through iOS 10).
22+
*
23+
* Use [UIImage as_imageNamed:] anywhere in your app, even if you aren't using other parts of ASDK.
24+
* Although not the best choice for extremely large assets that are only used once, it is the ideal
25+
* choice for any assets used in tab bars, nav bars, buttons, table or collection cells, etc.
26+
*/
27+
28+
@interface UIImage (ASDKFastImageNamed)
29+
30+
/**
31+
* A version of imageNamed that caches results because loading an image is expensive.
32+
* Calling with the same name value will usually return the same object. A UIImage,
33+
* after creation, is immutable and thread-safe so it's fine to share these objects across multiple threads.
34+
*
35+
* @param imageName The name of the image to load
36+
* @return The loaded image or nil
37+
*/
38+
+ (UIImage *)as_imageNamed:(NSString *)imageName;
39+
40+
/**
41+
* A version of imageNamed that caches results because loading an image is expensive.
42+
* Calling with the same name value will usually return the same object. A UIImage,
43+
* after creation, is immutable and thread-safe so it's fine to share these objects across multiple threads.
44+
*
45+
* @param imageName The name of the image to load
46+
* @param traitCollection The traits associated with the intended environment for the image.
47+
* @return The loaded image or nil
48+
*/
49+
+ (UIImage *)as_imageNamed:(NSString *)imageName compatibleWithTraitCollection:(nullable UITraitCollection *)traitCollection;
2450

25-
@interface UIImage (ASDKAdditions)
51+
@end
52+
53+
/**
54+
* High-performance flat-colored, rounded-corner resizable images
55+
*
56+
* For "Baked-in Opaque" corners, set cornerColor equal to the color behind the rounded image object,
57+
* i.e. the background color.
58+
* For "Baked-in Alpha" corners, set cornerColor = [UIColor clearColor]
59+
*
60+
* See http://asyncdisplaykit.org/docs/corner-rounding.html for an explanation.
61+
*/
62+
63+
@interface UIImage (ASDKResizableRoundedRects)
2664

2765
/**
2866
* This generates a flat-color, rounded-corner resizeable image
@@ -69,27 +107,6 @@ NS_ASSUME_NONNULL_BEGIN
69107
roundedCorners:(UIRectCorner)roundedCorners
70108
scale:(CGFloat)scale AS_WARN_UNUSED_RESULT;
71109

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

95112
NS_ASSUME_NONNULL_END

AsyncDisplayKit/UIImage+ASConvenience.m

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,56 @@
1414
#import <AsyncDisplayKit/ASInternalHelpers.h>
1515
#import <AsyncDisplayKit/ASAssert.h>
1616

17-
@implementation UIImage (ASDKAdditions)
17+
#pragma mark - ASDKFastImageNamed
18+
19+
@implementation UIImage (ASDKFastImageNamed)
20+
21+
UIImage *cachedImageNamed(NSString *imageName, UITraitCollection *traitCollection)
22+
{
23+
static NSCache *imageCache = nil;
24+
static dispatch_once_t onceToken;
25+
dispatch_once(&onceToken, ^{
26+
// Because NSCache responds to memory warnings, we do not need an explicit limit.
27+
// all of these objects contain compressed image data and are relatively small
28+
// compared to the backing stores of text and image views.
29+
imageCache = [[NSCache alloc] init];
30+
});
31+
32+
UIImage *image = nil;
33+
if ([imageName length] > 0) {
34+
NSString *imageKey = imageName;
35+
if (traitCollection) {
36+
char imageKeyBuffer[256];
37+
snprintf(imageKeyBuffer, sizeof(imageKeyBuffer), "%s|%ld|%ld", imageName.UTF8String, (long)traitCollection.horizontalSizeClass, (long)traitCollection.verticalSizeClass);
38+
imageKey = [NSString stringWithUTF8String:imageKeyBuffer];
39+
}
40+
41+
image = [imageCache objectForKey:imageKey];
42+
if (!image) {
43+
image = [UIImage imageNamed:imageName inBundle:nil compatibleWithTraitCollection:traitCollection];
44+
if (image) {
45+
[imageCache setObject:image forKey:imageKey];
46+
}
47+
}
48+
}
49+
return image;
50+
}
51+
52+
+ (UIImage *)as_imageNamed:(NSString *)imageName
53+
{
54+
return cachedImageNamed(imageName, nil);
55+
}
56+
57+
+ (UIImage *)as_imageNamed:(NSString *)imageName compatibleWithTraitCollection:(UITraitCollection *)traitCollection
58+
{
59+
return cachedImageNamed(imageName, traitCollection);
60+
}
61+
62+
@end
63+
64+
#pragma mark - ASDKResizableRoundedRects
65+
66+
@implementation UIImage (ASDKResizableRoundedRects)
1867

1968
+ (UIImage *)as_resizableRoundedImageWithCornerRadius:(CGFloat)cornerRadius
2069
cornerColor:(UIColor *)cornerColor
@@ -123,47 +172,4 @@ + (UIImage *)as_resizableRoundedImageWithCornerRadius:(CGFloat)cornerRadius
123172
return result;
124173
}
125174

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

0 commit comments

Comments
 (0)