diff --git a/android/src/main/java/com/horcrux/svg/ImageShadowNode.java b/android/src/main/java/com/horcrux/svg/ImageShadowNode.java index fbe7901e..e454a2c0 100644 --- a/android/src/main/java/com/horcrux/svg/ImageShadowNode.java +++ b/android/src/main/java/com/horcrux/svg/ImageShadowNode.java @@ -87,7 +87,12 @@ public class ImageShadowNode extends RenderableShadowNode { return; } - mImageRatio = (float)src.getInt("width") / (float)src.getInt("height"); + if (src.hasKey("width") && src.hasKey("height")) { + mImageRatio = (float)src.getInt("width") / (float)src.getInt("height"); + } + else { + mImageRatio = (float)0.0; + } mUri = Uri.parse(uriString); } } @@ -168,7 +173,7 @@ public class ImageShadowNode extends RenderableShadowNode { float rectRatio = rectWidth / rectHeight; RectF renderRect; - if (mImageRatio == rectRatio) { + if (mImageRatio == 0.0 || mImageRatio == rectRatio) { renderRect = new RectF(rect); } else if (mImageRatio < rectRatio) { renderRect = new RectF(0, 0, (int)(rectHeight * mImageRatio), (int)rectHeight); diff --git a/elements/Image.js b/elements/Image.js index 537381fe..65f1d348 100644 --- a/elements/Image.js +++ b/elements/Image.js @@ -18,7 +18,18 @@ export default class extends Shape { y: numberProp, width: numberProp.isRequired, height: numberProp.isRequired, - href: PropTypes.number.isRequired, + href: PropTypes.oneOfType([ + PropTypes.number, + function(props, propName, componentName) { + if (Object.keys(props[propName]).length != 1 || + !/^\s*data:([a-z]+\/[a-z]+(;[a-z\-]+\=[a-z\-]+)?)?(;base64)?,[a-z0-9\!\$\&\'\,\(\)\*\+\,\;\=\-\.\_\~\:\@\/\?\%\s]*\s*$/i.test(props[propName].uri)) { + return new Error( + 'Invalid prop `' + propName + '` supplied to' + + ' `' + componentName + '`. Validation failed.' + ); + } + } + ]).isRequired, preserveAspectRatio: PropTypes.string }; diff --git a/ios/Elements/RNSVGImage.m b/ios/Elements/RNSVGImage.m index 6a3a92a7..d5d714c7 100644 --- a/ios/Elements/RNSVGImage.m +++ b/ios/Elements/RNSVGImage.m @@ -26,7 +26,14 @@ _src = src; CGImageRelease(_image); RCTImageSource *source = [RCTConvert RCTImageSource:src]; - _imageRatio = source.size.width / source.size.height; + if (source.size.width != 0 && source.size.height != 0) + { + _imageRatio = source.size.width / source.size.height; + } + else + { + _imageRatio = 0.0 + } _image = CGImageRetain([RCTConvert CGImage:src]); [self invalidate]; } @@ -111,7 +118,7 @@ CGFloat rectRatio = rectWidth / rectHeight; CGRect renderRect; - if (imageRatio == rectRatio) { + if (imageRatio == 0.0 || imageRatio == rectRatio) { renderRect = rect; } else if (imageRatio < rectRatio) { renderRect = CGRectMake(0, 0, rectHeight * imageRatio, rectHeight);