Skip to content

Commit

Permalink
add rootDic for imageAsset
Browse files Browse the repository at this point in the history
  • Loading branch information
bellchen committed Apr 28, 2017
1 parent b29efe9 commit 2c06373
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 10 deletions.
27 changes: 19 additions & 8 deletions lottie-ios/Classes/AnimatableLayers/LOTLayerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -317,15 +317,26 @@ - (NSString*)description {
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR

- (void)_setImageForAsset {
if (_layerModel.imageAsset.imageName) {
NSArray *components = [_layerModel.imageAsset.imageName componentsSeparatedByString:@"."];
UIImage *image = [UIImage imageNamed:components.firstObject];
if (image) {
_childSolid.contents = (__bridge id _Nullable)(image.CGImage);
} else {
NSLog(@"%s: Warn: image not found: %@", __PRETTY_FUNCTION__, components.firstObject);
if (_layerModel.imageAsset.imageName) {
UIImage *image;
if (_layerModel.imageAsset.rootDir.length > 0) {
NSString *rootDic = _layerModel.imageAsset.rootDir;
if (_layerModel.imageAsset.imageDirectory.length > 0) {
rootDic = [rootDic stringByAppendingPathComponent:_layerModel.imageAsset.imageDirectory];
}
NSString *imagePath = [rootDic stringByAppendingPathComponent:_layerModel.imageAsset.imageName];
image = [UIImage imageWithContentsOfFile:imagePath];
}else{
NSArray *components = [_layerModel.imageAsset.imageName componentsSeparatedByString:@"."];
image = [UIImage imageNamed:components.firstObject];
}

if (image) {
_childSolid.contents = (__bridge id _Nullable)(image.CGImage);
} else {
NSLog(@"%s: Warn: image not found: %@", __PRETTY_FUNCTION__, _layerModel.imageAsset.imageName);
}
}
}
}

#else
Expand Down
1 change: 1 addition & 0 deletions lottie-ios/Classes/Models/LOTAsset.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ NS_ASSUME_NONNULL_BEGIN

@property (nonatomic, readonly, nullable) LOTLayerGroup *layerGroup;

@property (nonatomic, readwrite) NSString *rootDir;
@end

NS_ASSUME_NONNULL_END
1 change: 1 addition & 0 deletions lottie-ios/Classes/Models/LOTAssetGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@class LOTAsset;
@class LOTLayerGroup;
@interface LOTAssetGroup : NSObject
@property (nonatomic, readwrite) NSString * _Nullable rootDir;

- (instancetype _Nonnull)initWithJSON:(NSArray * _Nonnull)jsonArray;

Expand Down
7 changes: 6 additions & 1 deletion lottie-ios/Classes/Models/LOTAssetGroup.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,10 @@ - (void)finalizeInitialization {
- (LOTAsset *)assetModelForID:(NSString *)assetID {
return _assetMap[assetID];
}

- (void)setRootDir:(NSString *)rootDir{
_rootDir = rootDir;
[_assetMap enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, LOTAsset * _Nonnull obj, BOOL * _Nonnull stop) {
obj.rootDir = rootDir;
}];
}
@end
1 change: 1 addition & 0 deletions lottie-ios/Classes/Models/LOTComposition.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
@property (nonatomic, readonly) NSTimeInterval timeDuration;
@property (nonatomic, readonly) LOTLayerGroup *layerGroup;
@property (nonatomic, readonly) LOTAssetGroup *assetGroup;
@property (nonatomic, readwrite) NSString *rootDir;

@end
5 changes: 4 additions & 1 deletion lottie-ios/Classes/Models/LOTComposition.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,8 @@ - (void)_mapFromJSON:(NSDictionary *)jsonDictionary {
[_assetGroup finalizeInitialization];

}

- (void)setRootDir:(NSString *)rootDir{
_rootDir = rootDir;
self.assetGroup.rootDir = rootDir;
}
@end
25 changes: 25 additions & 0 deletions lottie-ios/Classes/Private/LOTAnimationView.m
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,31 @@ + (instancetype)animationFromJSON:(NSDictionary *)animationJSON {
return [[LOTAnimationView alloc] initWithModel:laScene];
}

+ (instancetype)animationWithFile:(NSString *)filePath{
NSString *animationName = filePath;

LOTComposition *comp = [[LOTAnimationCache sharedCache] animationForKey:animationName];
if (comp) {
return [[LOTAnimationView alloc] initWithModel:comp];
}

NSError *error;
NSData *jsonData = [[NSData alloc] initWithContentsOfFile:filePath];
NSDictionary *JSONObject = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData
options:0 error:&error] : nil;
if (JSONObject && !error) {
LOTComposition *laScene = [[LOTComposition alloc] initWithJSON:JSONObject];
laScene.rootDir = [filePath stringByDeletingLastPathComponent];
[[LOTAnimationCache sharedCache] addAnimation:laScene forKey:animationName];
return [[LOTAnimationView alloc] initWithModel:laScene];
}

NSException* resourceNotFoundException = [NSException exceptionWithName:@"ResourceNotFoundException"
reason:[error localizedDescription]
userInfo:nil];
@throw resourceNotFoundException;
}

- (instancetype)initWithContentsOfURL:(NSURL *)url {
self = [super initWithFrame:CGRectZero];
if (self) {
Expand Down
2 changes: 2 additions & 0 deletions lottie-ios/Classes/PublicHeaders/LOTAnimationView.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ typedef void (^LOTAnimationCompletionBlock)(BOOL animationFinished);

- (instancetype)initWithContentsOfURL:(NSURL *)url;

+ (instancetype)animationWithFile:(NSString *)filePath NS_SWIFT_NAME(init(file:));

@property (nonatomic, readonly) BOOL isAnimationPlaying;
@property (nonatomic, assign) BOOL loopAnimation;
@property (nonatomic, assign) CGFloat animationProgress;
Expand Down

0 comments on commit 2c06373

Please sign in to comment.