@@ -109,9 +109,29 @@ function resolveSource(source: ?Source): ImageSource {
109
109
let resolvedSource = { uri : '' } ;
110
110
111
111
if ( typeof source === 'number' ) {
112
- resolvedSource = resolveNumericSource ( source ) ;
112
+ // get the URI from the packager
113
+ const asset = getAssetByID ( source ) ;
114
+ if ( asset == null ) {
115
+ throw new Error (
116
+ `Image: asset with ID "${ source } " could not be found. Please check the image source or packager.`
117
+ ) ;
118
+ }
119
+ let scale = asset . scales [ 0 ] ;
120
+ if ( asset . scales . length > 1 ) {
121
+ const preferredScale = PixelRatio . get ( ) ;
122
+ // Get the scale which is closest to the preferred scale
123
+ scale = asset . scales . reduce ( ( prev , curr ) =>
124
+ Math . abs ( curr - preferredScale ) < Math . abs ( prev - preferredScale )
125
+ ? curr
126
+ : prev
127
+ ) ;
128
+ }
129
+
130
+ const scaleSuffix = scale !== 1 ? `@${ scale } x` : '' ;
131
+ const uri = `${ asset . httpServerLocation } /${ asset . name } ${ scaleSuffix } .${ asset . type } ` ;
132
+ resolvedSource = { uri, width : asset . width , height : asset . height } ;
113
133
} else if ( typeof source === 'string' ) {
114
- resolvedSource = resolveStringSource ( source ) ;
134
+ resolvedSource . uri = source ;
115
135
} else if ( Array . isArray ( source ) ) {
116
136
if ( process . env . NODE_ENV !== 'production' ) {
117
137
console . warn (
@@ -122,67 +142,23 @@ function resolveSource(source: ?Source): ImageSource {
122
142
123
143
return resolveSource ( source [ 0 ] ) ;
124
144
} else if ( source && typeof source . uri === 'string' ) {
125
- resolvedSource = resolveObjectSource ( source ) ;
145
+ const { uri, width, height, headers } = source ;
146
+ resolvedSource = { uri, width, height, headers } ;
126
147
}
127
148
128
149
if ( resolvedSource . uri ) {
129
150
const match = resolvedSource . uri . match ( svgDataUriPattern ) ;
151
+ // inline SVG markup may contain characters (e.g., #, ") that need to be escaped
130
152
if ( match ) {
131
- resolvedSource = resolveSvgDataUriSource ( resolvedSource , match ) ;
153
+ const [ , prefix , svg ] = match ;
154
+ const encodedSvg = encodeURIComponent ( svg ) ;
155
+ resolvedSource . uri = `${ prefix } ${ encodedSvg } ` ;
132
156
}
133
157
}
134
158
135
159
return resolvedSource ;
136
160
}
137
161
138
- // get the URI from the packager
139
- function resolveNumericSource ( source : number ) : ImageSource {
140
- const asset = getAssetByID ( source ) ;
141
- if ( asset == null ) {
142
- throw new Error (
143
- `Image: asset with ID "${ source } " could not be found. Please check the image source or packager.`
144
- ) ;
145
- }
146
- let scale = asset . scales [ 0 ] ;
147
- if ( asset . scales . length > 1 ) {
148
- const preferredScale = PixelRatio . get ( ) ;
149
- // Get the scale which is closest to the preferred scale
150
- scale = asset . scales . reduce ( ( prev , curr ) =>
151
- Math . abs ( curr - preferredScale ) < Math . abs ( prev - preferredScale )
152
- ? curr
153
- : prev
154
- ) ;
155
- }
156
-
157
- const scaleSuffix = scale !== 1 ? `@${ scale } x` : '' ;
158
- const uri = `${ asset . httpServerLocation } /${ asset . name } ${ scaleSuffix } .${ asset . type } ` ;
159
- const { height, width } = asset ;
160
-
161
- return { uri, height, width } ;
162
- }
163
-
164
- function resolveStringSource ( source : string ) : ImageSource {
165
- return { uri : source } ;
166
- }
167
-
168
- function resolveObjectSource ( source : Object ) : ImageSource {
169
- return ( source : ImageSource ) ;
170
- }
171
-
172
- function resolveSvgDataUriSource (
173
- source : Object ,
174
- match : Array < string >
175
- ) : ImageSource {
176
- const [ , prefix , svg ] = match ;
177
- // inline SVG markup may contain characters (e.g., #, ") that need to be escaped
178
- const encodedSvg = encodeURIComponent ( svg ) ;
179
-
180
- return {
181
- ...source ,
182
- uri : `${ prefix } ${ encodedSvg } `
183
- } ;
184
- }
185
-
186
162
// resolve any URI that might have a local blob URL created with `createObjectURL`
187
163
function resolveBlobUri ( source : ImageSource ) : string {
188
164
return ImageLoader . resolveBlobUri ( source . uri ) ;
0 commit comments