react-native-fast-image 8.31.1 → 8.31.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -138,7 +138,7 @@ When `source` changes, the image that's showing stays until the new one has load
138
138
 
139
139
  Remote url to load the image from. e.g. `'https://example.com/image.jpg'`.
140
140
 
141
- Also loads local files (`file://`, and on Android `content://`), photo library images on iOS (`ph://`, see [Photo library images](#photo-library-images-ios)) and SVG images (see [SVG images](docs/formats.md#svg-images)).
141
+ Also loads local files (`file://`, and on Android `content://`), images in the app by name (e.g. `'my_image'`: in its asset catalog on iOS, a drawable on Android), photo library images on iOS (`ph://`, see [Photo library images](#photo-library-images-ios)) and SVG images (see [SVG images](docs/formats.md#svg-images)).
142
142
 
143
143
  ---
144
144
 
package/dist/index.d.ts CHANGED
@@ -13,8 +13,9 @@ export type Source = {
13
13
  /**
14
14
  * Remote url to load the image from. e.g. `'https://example.com/image.jpg'`.
15
15
  *
16
- * Also loads local files (`file://`, and on Android `content://`), photo
17
- * library images on iOS (`ph://`, see
16
+ * Also loads local files (`file://`, and on Android `content://`), images
17
+ * in the app by name (e.g. `'my_image'`: in its asset catalog on iOS, a
18
+ * drawable on Android), photo library images on iOS (`ph://`, see
18
19
  * [Photo library images](#photo-library-images-ios)) and SVG images (see
19
20
  * [SVG images](docs/formats.md#svg-images)).
20
21
  */
@@ -62,6 +62,75 @@ static CGSize FFFPhotoPixelSize(NSURL *url)
62
62
  return size;
63
63
  }
64
64
 
65
+ // An image in the app by its name (`uri: 'my_image'`), such as one in the
66
+ // app's asset catalog, as React Native's Image takes: RCTConvert makes a uri
67
+ // without a scheme a file in the app's resources, where such an image isn't a
68
+ // file. The name, or nil if the url is a file that exists or isn't in the
69
+ // app's resources.
70
+ static NSString *FFFBundleAssetName(NSURL *url)
71
+ {
72
+ if (!url.isFileURL) {
73
+ return nil;
74
+ }
75
+ NSString *resources = [NSBundle.mainBundle.resourcePath stringByAppendingString:@"/"];
76
+ NSString *path = url.path;
77
+ if (![path hasPrefix:resources] || path.length == resources.length ||
78
+ [NSFileManager.defaultManager fileExistsAtPath:path]) {
79
+ return nil;
80
+ }
81
+ return [path substringFromIndex:resources.length];
82
+ }
83
+
84
+ // Loads images in the app by name (FFFBundleAssetName) with UIImage
85
+ // imageNamed:, which finds them in the asset catalog (and image files in the
86
+ // app's resources, e.g. with @2x and @3x versions), as React Native's Image
87
+ // does.
88
+ @interface FFFBundleAssetLoader : NSObject <SDImageLoader>
89
+ @end
90
+
91
+ @implementation FFFBundleAssetLoader
92
+
93
+ + (FFFBundleAssetLoader *)sharedLoader
94
+ {
95
+ static FFFBundleAssetLoader *loader;
96
+ static dispatch_once_t once;
97
+ dispatch_once(&once, ^{
98
+ loader = [FFFBundleAssetLoader new];
99
+ });
100
+ return loader;
101
+ }
102
+
103
+ - (BOOL)canRequestImageForURL:(NSURL *)url
104
+ {
105
+ return YES;
106
+ }
107
+
108
+ - (id<SDWebImageOperation>)requestImageWithURL:(NSURL *)url
109
+ options:(SDWebImageOptions)options
110
+ context:(SDWebImageContext *)context
111
+ progress:(SDImageLoaderProgressBlock)progressBlock
112
+ completed:(SDImageLoaderCompletedBlock)completedBlock
113
+ {
114
+ NSString *name = FFFBundleAssetName(url);
115
+ UIImage *image = name ? [UIImage imageNamed:name] : nil;
116
+ if (completedBlock) {
117
+ if (image) {
118
+ completedBlock(image, nil, nil, YES);
119
+ } else {
120
+ NSString *message = [NSString stringWithFormat:@"No image named %@ in the app", name ?: url.path];
121
+ completedBlock(nil, nil, [NSError errorWithDomain:SDWebImageErrorDomain code:SDWebImageErrorInvalidURL userInfo:@{NSLocalizedDescriptionKey: message}], YES);
122
+ }
123
+ }
124
+ return nil;
125
+ }
126
+
127
+ - (BOOL)shouldBlockFailedURLWithURL:(NSURL *)url error:(NSError *)error
128
+ {
129
+ return NO;
130
+ }
131
+
132
+ @end
133
+
65
134
  // Loads photo library images (ph://<localIdentifier>) with
66
135
  // SDWebImagePhotosPlugin's loader: a dependency on iOS (the podspec), found
67
136
  // at runtime so that tvOS builds without it (an app can add it there). Its
@@ -368,6 +437,9 @@ static id<SDImageCoder> FFFSVGCoder;
368
437
  if ([_url.scheme isEqualToString:@"assets-library"]) {
369
438
  return [FFFPhotosLoader assetsLibraryLoader];
370
439
  }
440
+ if (FFFBundleAssetName(_url)) {
441
+ return [FFFBundleAssetLoader sharedLoader];
442
+ }
371
443
  if (_cacheControl != FFFCacheControlWeb) {
372
444
  return nil;
373
445
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-fast-image",
3
- "version": "8.31.1",
3
+ "version": "8.31.2",
4
4
  "description": "FastImage, performant React Native image component.",
5
5
  "keywords": [
6
6
  "cache",