Skip to content

Commit 1d61937

Browse files
committed
Added .lottieResourcePath context and coder options, don't use NSBundle to make it more general (such as load it from sandbox)
1 parent 0a5c2f8 commit 1d61937

4 files changed

Lines changed: 83 additions & 8 deletions

File tree

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,28 @@ let pixelSize = CGSize(width: 300, height: 300)
149149
imageView.sd_setImage(with: lottieURL, placeholderImage: nil, options: [], contrext: [.thumbnailPixelSize : pixelSize])
150150
```
151151

152+
### Decoding
153+
154+
You can decode lottie image into aniamted UIImage/NSImage as well. If the lottie images have [referenced external image resource](https://airbnb.io/lottie/#/supported-features), you can specify it as well.
155+
156+
+ Objective-C
157+
158+
```objective-c
159+
// Lottie image decoding
160+
NSData *lottieJSONData;
161+
NSBundle *imageBundle; // You can even download the external image from online to local path, then load the lottie animation
162+
UIImage *image = [[SDImageLottieCoder sharedCoder] decodedImageWithData:lottieJSONData options:@{SDImageCoderDecodeLottieResourcePath : imageBundle.resourcePath}];
163+
```
164+
165+
+ Swift
166+
167+
```swift
168+
// Lottie image decoding
169+
let lottieJSONData: Data
170+
let imageBundle: Bundle // You can even download the external image from online to local path, then load the lottie animation
171+
let image = SDImageWebPCoder.shared.decodedImage(with: lottieJSONData, options: [.lottieResourcePath : imageBundle.resourcePath])
172+
```
173+
152174
## Screenshot
153175

154176
<img src="https://raw.githubusercontent.com/SDWebImage/SDWebImageLottieCoder/master/Example/Screenshot/LottieDemo.gif" width="300" />

SDWebImageLottieCoder/Classes/SDImageLottieCoder.m

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
#import "SDImageLottieCoder.h"
10+
#import "SDWebImageLottieLoaderDefine.h"
1011
#if __has_include(<rlottie/rlottie_capi.h>)
1112
#import <rlottie/rlottie_capi.h>
1213
#elif __has_include(<librlottie/librlottie.h>)
@@ -118,11 +119,17 @@ - (UIImage *)decodedImageWithData:(NSData *)data options:(SDImageCoderOptions *)
118119
if (preserveAspectRatioValue != nil) {
119120
preserveAspectRatio = preserveAspectRatioValue.boolValue;
120121
}
121-
NSBundle *bundle = NSBundle.mainBundle;
122-
const char *resourcePath = [bundle.resourcePath cStringUsingEncoding:NSUTF8StringEncoding];
122+
NSString *resourcePath = NSBundle.mainBundle.resourcePath;
123+
SDWebImageContext *context = options[SDImageCoderWebImageContext];
124+
if (context[SDWebImageContextLottieResourcePath]) {
125+
resourcePath = context[SDWebImageContextLottieResourcePath];
126+
} else if (options[SDImageCoderDecodeLottieResourcePath]) {
127+
resourcePath = options[SDImageCoderDecodeLottieResourcePath];
128+
}
123129
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
124-
const char *jsonData = [jsonString cStringUsingEncoding:NSUTF8StringEncoding];
125-
Lottie_Animation *animation = lottie_animation_from_data(jsonData, "", resourcePath);
130+
const char *jsonDataBuffer = [jsonString cStringUsingEncoding:NSUTF8StringEncoding];
131+
const char *resourcePathBuffer = [resourcePath cStringUsingEncoding:NSUTF8StringEncoding];
132+
Lottie_Animation *animation = lottie_animation_from_data(jsonDataBuffer, "", resourcePathBuffer);
126133
if (!animation) {
127134
return nil;
128135
}
@@ -200,11 +207,17 @@ - (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format o
200207
- (instancetype)initWithAnimatedImageData:(NSData *)data options:(SDImageCoderOptions *)options {
201208
self = [super init];
202209
if (self) {
203-
NSBundle *bundle = NSBundle.mainBundle;
204-
const char *resourcePath = [bundle.resourcePath cStringUsingEncoding:NSUTF8StringEncoding];
210+
NSString *resourcePath = NSBundle.mainBundle.resourcePath;
211+
SDWebImageContext *context = options[SDImageCoderWebImageContext];
212+
if (context[SDWebImageContextLottieResourcePath]) {
213+
resourcePath = context[SDWebImageContextLottieResourcePath];
214+
} else if (options[SDImageCoderDecodeLottieResourcePath]) {
215+
resourcePath = options[SDImageCoderDecodeLottieResourcePath];
216+
}
205217
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
206-
const char *jsonData = [jsonString cStringUsingEncoding:NSUTF8StringEncoding];
207-
Lottie_Animation *animation = lottie_animation_from_data(jsonData, "", resourcePath);
218+
const char *jsonDataBuffer = [jsonString cStringUsingEncoding:NSUTF8StringEncoding];
219+
const char *resourcePathBuffer = [resourcePath cStringUsingEncoding:NSUTF8StringEncoding];
220+
Lottie_Animation *animation = lottie_animation_from_data(jsonDataBuffer, "", resourcePathBuffer);
208221
if (!animation) {
209222
return nil;
210223
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* This file is part of the SDWebImage package.
3+
* (c) DreamPiggy <lizhuoli1126@126.com>
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
9+
#if __has_include(<SDWebImage/SDWebImage.h>)
10+
#import <SDWebImage/SDWebImage.h>
11+
#else
12+
@import SDWebImage;
13+
#endif
14+
15+
/**
16+
* The asset resource path used for lottie animation to load bitmap images in the animation. If you don't provide this context option, use main bundle's resource path instead.
17+
* Defaults to nil, means main bundle's resource path. (NSString)
18+
*/
19+
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextLottieResourcePath;
20+
21+
#pragma mark - Coder Options
22+
23+
/**
24+
* The asset resource path used for lottie animation to load bitmap images in the animation. If you don't provide this context option, use main bundle's resource path instead.
25+
* Defaults to nil, means main bundle's resource path. (NSString)
26+
*/
27+
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderDecodeLottieResourcePath;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* This file is part of the SDWebImage package.
3+
* (c) DreamPiggy <lizhuoli1126@126.com>
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
9+
#import "SDWebImageLottieLoaderDefine.h"
10+
11+
SDWebImageContextOption _Nonnull const SDWebImageContextLottieResourcePath = @"lottieResourcePath";
12+
13+
SDImageCoderOption _Nonnull const SDImageCoderDecodeLottieResourcePath = @"decodeLottieResourcePath";

0 commit comments

Comments
 (0)