Skip to content
Merged
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
18 changes: 17 additions & 1 deletion SDWebImageLottieCoder/Classes/SDImageLottieCoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#else
@import librlottie;
#endif
#import <CommonCrypto/CommonDigest.h>

#define SD_TWO_CC(c1,c2) ((uint16_t)(((c2) << 8) | (c1)))

Expand Down Expand Up @@ -58,6 +59,18 @@ static CGSize SDCalculateThumbnailSize(CGSize fullSize, BOOL preserveAspectRatio
return CGSizeMake(resultWidth, resultHeight);
}

static NSString *SDCalculateMD5(NSString *input) {
const char* str = [input UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(str, (CC_LONG)strlen(str), result);

NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH*2];
for(int i = 0; i<CC_MD5_DIGEST_LENGTH; i++) {
[ret appendFormat:@"%02x",result[i]];
}
return ret;
}

@implementation SDImageLottieCoder {
CGFloat _scale;
Lottie_Animation * _animation;
Expand Down Expand Up @@ -225,7 +238,10 @@ - (instancetype)initWithAnimatedImageData:(NSData *)data options:(SDImageCoderOp
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
const char *jsonDataBuffer = [jsonString cStringUsingEncoding:NSUTF8StringEncoding];
const char *resourcePathBuffer = [resourcePath cStringUsingEncoding:NSUTF8StringEncoding];
Lottie_Animation *animation = lottie_animation_from_data(jsonDataBuffer, "", resourcePathBuffer);
// rlottie C API use cache by default, so we must calculate cache key
NSString *cacheKey = SDCalculateMD5(jsonString);
const char *cacheKeyBuffer = [cacheKey cStringUsingEncoding:NSUTF8StringEncoding];
Lottie_Animation *animation = lottie_animation_from_data(jsonDataBuffer, cacheKeyBuffer, resourcePathBuffer);
if (!animation) {
return nil;
}
Expand Down