Skip to content

Commit 1cbf763

Browse files
author
farfromrefug
committed
fix(svg): handle src change to null and clear image
1 parent 09f9c30 commit 1cbf763

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

src/ui-svg/index.android.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ function getSDK() {
1111
return SDK_INT;
1212
}
1313
function getSVG(src: string | ImageAsset | File) {
14+
if (!src) {
15+
return null;
16+
}
1417
let imagePath: string;
1518
if (src instanceof File) {
1619
return com.caverock.androidsvg.SVG.getFromInputStream(new java.io.FileInputStream(new java.io.File(src.path)));
@@ -402,12 +405,20 @@ export class SVGView extends SVGViewBase {
402405
}
403406
async handleSrc(src) {
404407
if (src instanceof Promise) {
405-
this.handleSrc(await src);
408+
try {
409+
this.handleSrc(await src);
410+
} catch (error) {
411+
this.handleSrc(null);
412+
}
406413
return;
407414
} else if (typeof src === 'function') {
408-
const newSrc = src();
415+
let newSrc = src();
409416
if (newSrc instanceof Promise) {
410-
await newSrc;
417+
try {
418+
await newSrc;
419+
} catch (error) {
420+
newSrc = null;
421+
}
411422
}
412423
this.handleSrc(newSrc);
413424
return;

src/ui-svg/index.ios.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ function getUIImageScaleType(scaleType: string) {
2121

2222
let bgdImagePaint: Paint;
2323
function getRenderer(src: string | ImageAsset | File) {
24+
if (!src) {
25+
return null;
26+
}
2427
let imagePath: string;
2528
if (src instanceof File) {
2629
return SVGRenderer.alloc().initWithInputStream(NSInputStream.alloc().initWithFileAtPath(src.path));
@@ -45,6 +48,9 @@ function getRenderer(src: string | ImageAsset | File) {
4548
return SVGRenderer.alloc().initWithString(imagePath);
4649
}
4750
function getSVGKImage(src: string | ImageAsset | File) {
51+
if (!src) {
52+
return null;
53+
}
4854
let imagePath: string;
4955
if (src instanceof File) {
5056
return SVGKImage.alloc().initWithData(NSData.alloc().initWithContentsOfFile(src.path));
@@ -322,12 +328,20 @@ export class SVGView extends SVGViewBase {
322328
}
323329
async handleSrc(src) {
324330
if (src instanceof Promise) {
325-
this.handleSrc(await src);
331+
try {
332+
this.handleSrc(await src);
333+
} catch (error) {
334+
this.handleSrc(null);
335+
}
326336
return;
327337
} else if (typeof src === 'function') {
328-
const newSrc = src();
338+
let newSrc = src();
329339
if (newSrc instanceof Promise) {
330-
await newSrc;
340+
try {
341+
await newSrc;
342+
} catch (error) {
343+
newSrc = null;
344+
}
331345
}
332346
this.handleSrc(newSrc);
333347
return;

0 commit comments

Comments
 (0)