Merge pull request #216 from gpminsuk/master

Data URI support for href in react-native-svg Image tag
This commit is contained in:
Horcrux
2017-04-04 10:41:35 +08:00
committed by GitHub
3 changed files with 28 additions and 5 deletions

View File

@@ -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);

View File

@@ -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
};

View File

@@ -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);