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

完善缓存方式 #134

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- Protobuf (3.6.1)
- Protobuf (3.12.0)
- React/ART (0.45.1):
- React/Core
- React/Core (0.45.1):
Expand Down Expand Up @@ -27,8 +27,8 @@ PODS:
- React/Core
- React/RCTWebSocket (0.45.1):
- React/Core
- SSZipArchive (2.1.4)
- Yoga (1.9.0)
- SSZipArchive (2.1.5)
- Yoga (1.14.0)

DEPENDENCIES:
- Protobuf (~> 3.4)
Expand All @@ -47,7 +47,7 @@ DEPENDENCIES:
- Yoga

SPEC REPOS:
https://github.com/cocoapods/specs.git:
trunk:
- Protobuf
- SSZipArchive
- Yoga
Expand All @@ -57,11 +57,11 @@ EXTERNAL SOURCES:
:podspec: https://raw.githubusercontent.com/yyued/react-native-runtime-ios/0.45.1/React.podspec

SPEC CHECKSUMS:
Protobuf: 1eb9700044745f00181c136ef21b8ff3ad5a0fd5
Protobuf: 2793fcd0622a00b546c60e7cbbcc493e043e9bb9
React: 505e0132cd9aaba1a56e47ef509220dd794ec9be
SSZipArchive: 41455d4b8d2b6ab93990820b50dc697c2554a322
Yoga: aaae8abea68951f60bee05f6277d3eed90bb91bb
SSZipArchive: cefe1364104a0231268a5deb8495bdf2861f52f0
Yoga: cff67a400f6b74dc38eb0bad4f156673d9aa980c

PODFILE CHECKSUM: 7f6714245d47e69d2933463289e4c4d6de65b831

COCOAPODS: 1.7.4
COCOAPODS: 1.9.1
6 changes: 6 additions & 0 deletions SVGAPlayer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
90D7CA1C1F7FB114006E74F0 /* rose_1.5.0.svga in Resources */ = {isa = PBXBuildFile; fileRef = 90D7CA1A1F7FB114006E74F0 /* rose_1.5.0.svga */; };
90D7CA1E1F7FB34E006E74F0 /* libz.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 90D7CA1D1F7FB34E006E74F0 /* libz.tbd */; };
90DB59B51F96026E00894727 /* SVGAImageView.m in Sources */ = {isa = PBXBuildFile; fileRef = 90DB59B41F96026E00894727 /* SVGAImageView.m */; };
FA7FA4DF24B09BE600413DF0 /* SVGAVideoMemoryCache.m in Sources */ = {isa = PBXBuildFile; fileRef = FA7FA4DE24B09BE600413DF0 /* SVGAVideoMemoryCache.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -119,6 +120,8 @@
90DB59B41F96026E00894727 /* SVGAImageView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SVGAImageView.m; sourceTree = "<group>"; };
92332F7A897BF4379D765B05 /* libPods-SVGAPlayer React.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-SVGAPlayer React.a"; sourceTree = BUILT_PRODUCTS_DIR; };
E02B8713B25C0283C736EE03 /* Pods-SVGAPlayer.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SVGAPlayer.release.xcconfig"; path = "Pods/Target Support Files/Pods-SVGAPlayer/Pods-SVGAPlayer.release.xcconfig"; sourceTree = "<group>"; };
FA7FA4DD24B09BE600413DF0 /* SVGAVideoMemoryCache.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SVGAVideoMemoryCache.h; sourceTree = "<group>"; };
FA7FA4DE24B09BE600413DF0 /* SVGAVideoMemoryCache.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SVGAVideoMemoryCache.m; sourceTree = "<group>"; };
FF89C40C3E9839DA5DE71191 /* Pods-SVGAPlayer React.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SVGAPlayer React.release.xcconfig"; path = "Pods/Target Support Files/Pods-SVGAPlayer React/Pods-SVGAPlayer React.release.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -233,6 +236,8 @@
9052FC621E6EB8D4007BC925 /* SVGAExporter.m */,
904D41F61D223DD20085A21A /* SVGABezierPath.h */,
904D41F71D223DD20085A21A /* SVGABezierPath.m */,
FA7FA4DD24B09BE600413DF0 /* SVGAVideoMemoryCache.h */,
FA7FA4DE24B09BE600413DF0 /* SVGAVideoMemoryCache.m */,
);
path = Source;
sourceTree = "<group>";
Expand Down Expand Up @@ -434,6 +439,7 @@
buildActionMask = 2147483647;
files = (
90A676E81D13A6DF008A69F3 /* ViewController.m in Sources */,
FA7FA4DF24B09BE600413DF0 /* SVGAVideoMemoryCache.m in Sources */,
9052FC631E6EB8D4007BC925 /* SVGAExporter.m in Sources */,
90A677031D13AE19008A69F3 /* SVGAVideoEntity.m in Sources */,
90A364DA1E5AED04009347F1 /* SVGAVideoSpriteFrameEntity.m in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>SVGAPlayer React.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>7</integer>
</dict>
<key>SVGAPlayer.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
uuid = "90735387-BC57-48F9-B2A6-8E3BD7738622"
type = "0"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "C3B012C7-64E1-41D1-A302-55BF3023C28C"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Source/SVGAVideoMemoryCache.m"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "148"
endingLineNumber = "148"
landmarkName = "-cache:willEvictObject:"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
7 changes: 7 additions & 0 deletions Source/SVGAParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ + (void)load {
unzipQueue.maxConcurrentOperationCount = 1;
}

- (instancetype)init {
if (self = [super init]) {
_enabledMemoryCache = YES;
}
return self;
}

- (void)parseWithURL:(nonnull NSURL *)URL
completionBlock:(void ( ^ _Nonnull )(SVGAVideoEntity * _Nullable videoItem))completionBlock
failureBlock:(void ( ^ _Nullable)(NSError * _Nullable error))failureBlock {
Expand Down
1 change: 1 addition & 0 deletions Source/SVGAVideoEntity.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
@property (nonatomic, readonly) NSDictionary<NSString *, NSData *> *audiosData;
@property (nonatomic, readonly) NSArray<SVGAVideoSpriteEntity *> *sprites;
@property (nonatomic, readonly) NSArray<SVGAAudioEntity *> *audios;
@property (nonatomic, readonly) NSUInteger memoryCost;

- (instancetype)initWithJSONObject:(NSDictionary *)JSONObject cacheDir:(NSString *)cacheDir;
- (void)resetImagesWithJSONObject:(NSDictionary *)JSONObject;
Expand Down
55 changes: 44 additions & 11 deletions Source/SVGAVideoEntity.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import "SVGAVideoSpriteEntity.h"
#import "SVGAAudioEntity.h"
#import "Svga.pbobjc.h"
#import "SVGAVideoMemoryCache.h"

#define MP3_MAGIC_NUMBER "ID3"

Expand All @@ -26,19 +27,13 @@ @interface SVGAVideoEntity ()
@property (nonatomic, copy) NSArray<SVGAAudioEntity *> *audios;
@property (nonatomic, copy) NSString *cacheDir;

@property (nonatomic, copy) NSString * cacheKey;
@property (nonatomic, readwrite) NSUInteger memoryCost;

@end

@implementation SVGAVideoEntity

static NSCache *videoCache;

+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
videoCache = [[NSCache alloc] init];
});
}

- (instancetype)initWithJSONObject:(NSDictionary *)JSONObject cacheDir:(NSString *)cacheDir {
self = [super init];
if (self) {
Expand Down Expand Up @@ -202,12 +197,50 @@ - (void)resetAudiosWithProtoObject:(SVGAProtoMovieEntity *)protoObject {
self.audios = audios;
}

#pragma mark - memory

- (void)setImages:(NSDictionary<NSString *,UIImage *> *)images {
_images = images;

// cache存在说明以缓存,本次更新内存占用情况即可
if (_cacheKey) {
NSUInteger formerCost = self.memoryCost;
[self resetMemoryCost];
[[SVGAVideoMemoryCache sharedCache] updateCost:self.memoryCost - formerCost];
} else {
[self resetMemoryCost];
}
}

+ (SVGAVideoEntity *)readCache:(NSString *)cacheKey {
return [videoCache objectForKey:cacheKey];
return [[SVGAVideoMemoryCache sharedCache] objectForKey:cacheKey];
}

- (void)saveCache:(NSString *)cacheKey {
[videoCache setObject:self forKey:cacheKey];
_cacheKey = cacheKey;
[[SVGAVideoMemoryCache sharedCache] setObject:self forKey:cacheKey];
}

/// 重置内存占用计算
- (void)resetMemoryCost {
_memoryCost = 0;

[self.images enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, UIImage * _Nonnull obj, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:[UIImage class]]) {
NSUInteger cost = [self costForImage:obj];
_memoryCost += cost;
}
}];
}

- (NSUInteger)costForImage:(UIImage *)image {
CGImageRef imageRef = image.CGImage;
if (!imageRef) {
return 0;
}
NSUInteger bytesPerFrame = CGImageGetBytesPerRow(imageRef) * CGImageGetHeight(imageRef);
NSUInteger frameCount = image.images.count > 0 ? image.images.count : 1;
return bytesPerFrame * frameCount;
}

@end
Expand Down
54 changes: 54 additions & 0 deletions Source/SVGAVideoMemoryCache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// SVGAVideoMemoryCache.h
// SVGAPlayer
//
// Created by MOMO@song.meng on 2020/7/4.
// Copyright © 2020 UED Center. All rights reserved.
//
// 用于处理SVGA内存缓存
//

#import <Foundation/Foundation.h>
#import "SVGAVideoEntity.h"
#import "SVGAVideoEntity.h"

NS_ASSUME_NONNULL_BEGIN

@interface SVGAVideoMemoryCache : NSObject


/// 是否显示内存占用tip,开启统计后才生效,默认为NO,debug环境下默认为YES
@property (nonatomic, assign) BOOL showMemoryCostTip;

/// 设置内存占用显示位置
@property (nonatomic, assign) CGRect memoryCostTipFrame;

/// 内存使用限制
@property (nonatomic, assign) NSUInteger memoryCostLimit;

/// 内存使用情况,在statisticsMemoryCost为YES的情况下有效
@property (nonatomic, readonly) NSUInteger totalMemoryCost;

/// 内存缓存是否生效,默认为YES,为NO时将使用weak缓存
/// 以避免同一时刻多个SVGAPlayer使用同一资源造成重复的内存占用
/// 关闭后内存使用统计和内存占用提示将失效
@property (nonatomic, assign) BOOL memoryCacheEnable;

/// 退后台是否自动清理,默认为YES
@property (nonatomic, assign) BOOL clearInBackground;

+ (instancetype)sharedCache;


- (void)setObject:(SVGAVideoEntity *)object forKey:(id)key;
- (SVGAVideoEntity *)objectForKey:(id)key;
- (void)removeAllObjects;
- (void)removeObjectForKey:(id)key;

/// 当图片资源被替换时更新内存占用
/// @param cost 内存占用情况
- (void)updateCost:(NSInteger)cost;

@end

NS_ASSUME_NONNULL_END
Loading