Skip to content

Commit

Permalink
增加图片缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
lixiang1994 committed Sep 6, 2024
1 parent 8da92fb commit e858b2f
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 30 deletions.
2 changes: 1 addition & 1 deletion LEETheme.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "LEETheme"
s.version = "1.2.1"
s.version = "1.2.2"
s.summary = "最好用的轻量级主题管理库"

s.homepage = "https://github.com/lixiang1994/LEETheme"
Expand Down
8 changes: 7 additions & 1 deletion LEETheme/LEETheme.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @author LEE
* @copyright Copyright © 2016 - 2024年 lee. All rights reserved.
* @version V1.2.1
* @version V1.2.2
*/

#import <Foundation/Foundation.h>
Expand All @@ -21,6 +21,8 @@

#import "LEEThemeHelper.h"

#import "LEEThemeImageCache.h"

/*
*********************************************************************************
Expand Down Expand Up @@ -70,6 +72,10 @@ NS_ASSUME_NONNULL_BEGIN
*/
+ (NSArray *)allThemeTag;

/// 图片缓存
+ (LEEThemeImageCache *)imageCache;
+ (void)setImageCache:(LEEThemeImageCache *)imageCache;

@end

@interface LEETheme (JsonModeExtend)
Expand Down
31 changes: 28 additions & 3 deletions LEETheme/LEETheme.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @author LEE
* @copyright Copyright © 2016 - 2024年 lee. All rights reserved.
* @version V1.2.1
* @version V1.2.2
*/

#import "LEETheme.h"
Expand All @@ -37,6 +37,8 @@ @interface LEETheme ()

@property (nonatomic , strong ) NSMutableDictionary *configInfo;

@property (nonatomic , strong ) LEEThemeImageCache *imageCache;

@end

@implementation LEETheme
Expand All @@ -51,12 +53,21 @@ + (LEETheme *)shareTheme{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{

themeManager = [[LEETheme alloc]init];
themeManager = [[LEETheme alloc] init];
});

return themeManager;
}

- (instancetype)init
{
self = [super init];
if (self) {
_imageCache = [[LEEThemeImageCache alloc] init];
}
return self;
}

#pragma mark Public

+ (void)startTheme:(NSString *)tag{
Expand Down Expand Up @@ -89,6 +100,16 @@ + (NSArray *)allThemeTag{
return [[LEETheme shareTheme].allTags copy];
}

+ (LEEThemeImageCache *)imageCache {

return [LEETheme shareTheme].imageCache;
}

+ (void)setImageCache:(LEEThemeImageCache *)imageCache {

[LEETheme shareTheme].imageCache = [imageCache copy];
}

#pragma mark Private

- (void)setCurrentTag:(NSString *)currentTag{
Expand Down Expand Up @@ -229,19 +250,23 @@ + (id)getValueWithTag:(NSString *)tag Identifier:(NSString *)identifier{

if (imageName) {

UIImage *image = [[LEETheme shareTheme].imageCache getImageWithKey:imageName];

NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;

NSString *path = configInfo[@"path"];

if (path) path = [documentsPath stringByAppendingPathComponent:path];

UIImage *image = path ? [UIImage imageWithContentsOfFile:[path stringByAppendingPathComponent:imageName]] : [UIImage imageNamed:imageName];
if (!image) image = path ? [UIImage imageWithContentsOfFile:[path stringByAppendingPathComponent:imageName]] : [UIImage imageNamed:imageName];

if (!image) image = [UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:imageName]];

if (!image) image = [UIImage imageNamed:imageName];

if (image && !value) value = image;

[[LEETheme shareTheme].imageCache setImage:image withKey:imageName];
}

NSDictionary *otherInfo = info[@"other"];
Expand Down
2 changes: 1 addition & 1 deletion LEETheme/LEEThemeHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @author LEE
* @copyright Copyright © 2016 - 2024年 lee. All rights reserved.
* @version V1.2.1
* @version V1.2.2
*/

FOUNDATION_EXPORT double LEEThemeVersionNumber;
Expand Down
32 changes: 32 additions & 0 deletions LEETheme/LEEThemeImageCache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* @header LEEThemeImageCache.h
*
* ┌─┐ ┌───────┐ ┌───────┐
* │ │ │ ┌─────┘ │ ┌─────┘
* │ │ │ └─────┐ │ └─────┐
* │ │ │ ┌─────┘ │ ┌─────┘
* │ └─────┐│ └─────┐ │ └─────┐
* └───────┘└───────┘ └───────┘
*
* @brief LEE主题管理
*
* @author LEE
* @copyright Copyright © 2016 - 2024年 lee. All rights reserved.
* @version V1.2.2
*/

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface LEEThemeImageCache : NSObject

- (UIImage *)getImageWithKey:(NSString *)key;

- (void)setImage:(UIImage *)image withKey:(NSString *)key;

- (void)clean;

@end

NS_ASSUME_NONNULL_END
49 changes: 49 additions & 0 deletions LEETheme/LEEThemeImageCache.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* @header LEEThemeImageCache.m
*
* ┌─┐ ┌───────┐ ┌───────┐
* │ │ │ ┌─────┘ │ ┌─────┘
* │ │ │ └─────┐ │ └─────┐
* │ │ │ ┌─────┘ │ ┌─────┘
* │ └─────┐│ └─────┐ │ └─────┐
* └───────┘└───────┘ └───────┘
*
* @brief LEE主题管理
*
* @author LEE
* @copyright Copyright © 2016 - 2024年 lee. All rights reserved.
* @version V1.2.2
*/

#import "LEEThemeImageCache.h"

@interface LEEThemeImageCache ()

@property (nonatomic , strong ) NSMutableDictionary *cache;

@end

@implementation LEEThemeImageCache

- (instancetype)init
{
self = [super init];
if (self) {
_cache = [NSMutableDictionary dictionary];
}
return self;
}

- (UIImage *)getImageWithKey:(NSString *)key {
return self.cache[key];
}

- (void)setImage:(UIImage *)image withKey:(NSString *)key {
self.cache[key] = image;
}

- (void)clean {
[self.cache removeAllObjects];
}

@end
10 changes: 8 additions & 2 deletions LEEThemeDemo/LEEThemeDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
9B1415261EE02EF600173BB9 /* image_gray@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 9B1415251EE02EF600173BB9 /* image_gray@2x.png */; };
9B1415281EE02F0C00173BB9 /* true_gray@3x.png in Resources */ = {isa = PBXBuildFile; fileRef = 9B1415271EE02F0C00173BB9 /* true_gray@3x.png */; };
9B14152A1EE02F1A00173BB9 /* true_gray@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 9B1415291EE02F1A00173BB9 /* true_gray@2x.png */; };
9B5711B12C8B208500093AE9 /* LEEThemeImageCache.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B5711B02C8B208500093AE9 /* LEEThemeImageCache.m */; };
9B7873E51E7A3F4A00D4151A /* UITableView+SDAutoTableViewCellHeight.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B7873301E7A3F4A00D4151A /* UITableView+SDAutoTableViewCellHeight.m */; };
9B7873E61E7A3F4A00D4151A /* UIView+SDAutoLayout.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B7873321E7A3F4A00D4151A /* UIView+SDAutoLayout.m */; };
9B7875071E7A5AF800D4151A /* UINavigationController+FDFullscreenPopGesture.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B7875061E7A5AF800D4151A /* UINavigationController+FDFullscreenPopGesture.m */; };
Expand Down Expand Up @@ -445,6 +446,8 @@
9B1415251EE02EF600173BB9 /* image_gray@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "image_gray@2x.png"; sourceTree = "<group>"; };
9B1415271EE02F0C00173BB9 /* true_gray@3x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "true_gray@3x.png"; sourceTree = "<group>"; };
9B1415291EE02F1A00173BB9 /* true_gray@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "true_gray@2x.png"; sourceTree = "<group>"; };
9B5711AF2C8B208500093AE9 /* LEEThemeImageCache.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LEEThemeImageCache.h; sourceTree = "<group>"; };
9B5711B02C8B208500093AE9 /* LEEThemeImageCache.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LEEThemeImageCache.m; sourceTree = "<group>"; };
9B78732E1E7A3F4A00D4151A /* SDAutoLayout.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDAutoLayout.h; sourceTree = "<group>"; };
9B78732F1E7A3F4A00D4151A /* UITableView+SDAutoTableViewCellHeight.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UITableView+SDAutoTableViewCellHeight.h"; sourceTree = "<group>"; };
9B7873301E7A3F4A00D4151A /* UITableView+SDAutoTableViewCellHeight.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UITableView+SDAutoTableViewCellHeight.m"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2444,6 +2447,8 @@
9B9712C71EC42C1C00C1CB79 /* LEETheme.h */,
9B9712C81EC42C1C00C1CB79 /* LEETheme.m */,
9B1081321F42CBA400D3A7E2 /* LEEThemeHelper.h */,
9B5711AF2C8B208500093AE9 /* LEEThemeImageCache.h */,
9B5711B02C8B208500093AE9 /* LEEThemeImageCache.m */,
);
name = LEETheme;
path = ../../LEETheme;
Expand Down Expand Up @@ -2969,6 +2974,7 @@
9B78781C1E7B8BCF00D4151A /* CommunityPostCellPhotosView.m in Sources */,
9B7879711E7B92DE00D4151A /* YYRootViewController.m in Sources */,
9B7876631E7A739200D4151A /* YYImageCache.m in Sources */,
9B5711B12C8B208500093AE9 /* LEEThemeImageCache.m in Sources */,
9B78763A1E7A739200D4151A /* NSBundle+YYAdd.m in Sources */,
9B7878211E7B8BCF00D4151A /* CommunityCircleDetailsViewController.m in Sources */,
9B7876441E7A739200D4151A /* NSString+YYAdd.m in Sources */,
Expand Down Expand Up @@ -3197,7 +3203,7 @@
);
GCC_PREFIX_HEADER = "$(SRCROOT)/LEEThemeDemo/PrefixHeader.pch";
INFOPLIST_FILE = LEEThemeDemo/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -3226,7 +3232,7 @@
);
GCC_PREFIX_HEADER = "$(SRCROOT)/LEEThemeDemo/PrefixHeader.pch";
INFOPLIST_FILE = LEEThemeDemo/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
48 changes: 26 additions & 22 deletions LEEThemeDemo/LEEThemeDemo/Librarys/YYKit/Utility/YYAsyncLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -190,28 +190,32 @@ - (void)_displayAsync:(BOOL)async {
} else {
[_sentinel increase];
if (task.willDisplay) task.willDisplay(self);
UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, self.contentsScale);
CGContextRef context = UIGraphicsGetCurrentContext();
if (self.opaque) {
CGSize size = self.bounds.size;
size.width *= self.contentsScale;
size.height *= self.contentsScale;
CGContextSaveGState(context); {
if (!self.backgroundColor || CGColorGetAlpha(self.backgroundColor) < 1) {
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextAddRect(context, CGRectMake(0, 0, size.width, size.height));
CGContextFillPath(context);
}
if (self.backgroundColor) {
CGContextSetFillColorWithColor(context, self.backgroundColor);
CGContextAddRect(context, CGRectMake(0, 0, size.width, size.height));
CGContextFillPath(context);
}
} CGContextRestoreGState(context);
}
task.display(context, self.bounds.size, ^{return NO;});
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
format.opaque = self.opaque;
format.scale = self.contentsScale;

UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:self.bounds.size format:format];
UIImage *image = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
CGContextRef context = rendererContext.CGContext;
if (self.opaque) {
CGSize size = self.bounds.size;
size.width *= self.contentsScale;
size.height *= self.contentsScale;
CGContextSaveGState(context); {
if (!self.backgroundColor || CGColorGetAlpha(self.backgroundColor) < 1) {
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextAddRect(context, CGRectMake(0, 0, size.width, size.height));
CGContextFillPath(context);
}
if (self.backgroundColor) {
CGContextSetFillColorWithColor(context, self.backgroundColor);
CGContextAddRect(context, CGRectMake(0, 0, size.width, size.height));
CGContextFillPath(context);
}
} CGContextRestoreGState(context);
}
task.display(context, self.bounds.size, ^{return NO;});
}];
self.contents = (__bridge id)(image.CGImage);
if (task.didDisplay) task.didDisplay(self, YES);
}
Expand Down
4 changes: 4 additions & 0 deletions UPDATELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

# LEETheme - 更新日志

V1.2.2
==============
增加图片缓存

V1.2.1
==============
添加隐私清单
Expand Down

0 comments on commit e858b2f

Please sign in to comment.