From 9df76e5668bffbd79c0c4bcfd9c610d8c2b9d7af Mon Sep 17 00:00:00 2001 From: Horcrux Date: Tue, 4 Apr 2017 10:45:54 +0800 Subject: [PATCH] Use Image.propTypes.source as href propType --- .../main/java/com/horcrux/svg/ImageShadowNode.java | 7 +++---- elements/Image.js | 14 ++------------ ios/Elements/RNSVGImage.m | 12 +++++------- 3 files changed, 10 insertions(+), 23 deletions(-) diff --git a/android/src/main/java/com/horcrux/svg/ImageShadowNode.java b/android/src/main/java/com/horcrux/svg/ImageShadowNode.java index e454a2c0..14b470b7 100644 --- a/android/src/main/java/com/horcrux/svg/ImageShadowNode.java +++ b/android/src/main/java/com/horcrux/svg/ImageShadowNode.java @@ -89,9 +89,8 @@ public class ImageShadowNode extends RenderableShadowNode { if (src.hasKey("width") && src.hasKey("height")) { mImageRatio = (float)src.getInt("width") / (float)src.getInt("height"); - } - else { - mImageRatio = (float)0.0; + } else { + mImageRatio = 0f; } mUri = Uri.parse(uriString); } @@ -173,7 +172,7 @@ public class ImageShadowNode extends RenderableShadowNode { float rectRatio = rectWidth / rectHeight; RectF renderRect; - if (mImageRatio == 0.0 || mImageRatio == rectRatio) { + if (mImageRatio == 0f || 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 65f1d348..673ab9ca 100644 --- a/elements/Image.js +++ b/elements/Image.js @@ -1,4 +1,5 @@ import React, {PropTypes} from 'react'; +import { Image } from 'react-native'; import createReactNativeComponentClass from 'react-native/Libraries/Renderer/src/renderers/native/createReactNativeComponentClass'; import {ImageAttributes} from '../lib/attributes'; import {numberProp, touchableProps, responderProps} from '../lib/props'; @@ -18,18 +19,7 @@ export default class extends Shape { y: numberProp, width: numberProp.isRequired, height: numberProp.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, + href: Image.propTypes.source, preserveAspectRatio: PropTypes.string }; diff --git a/ios/Elements/RNSVGImage.m b/ios/Elements/RNSVGImage.m index d5d714c7..97ac9f62 100644 --- a/ios/Elements/RNSVGImage.m +++ b/ios/Elements/RNSVGImage.m @@ -26,14 +26,12 @@ _src = src; CGImageRelease(_image); RCTImageSource *source = [RCTConvert RCTImageSource:src]; - if (source.size.width != 0 && source.size.height != 0) - { + if (source.size.width != 0 && source.size.height != 0) { _imageRatio = source.size.width / source.size.height; + } else { + _imageRatio = 0.0; } - else - { - _imageRatio = 0.0 - } + _image = CGImageRetain([RCTConvert CGImage:src]); [self invalidate]; } @@ -118,7 +116,7 @@ CGFloat rectRatio = rectWidth / rectHeight; CGRect renderRect; - if (imageRatio == 0.0 || imageRatio == rectRatio) { + if (!imageRatio || imageRatio == rectRatio) { renderRect = rect; } else if (imageRatio < rectRatio) { renderRect = CGRectMake(0, 0, rectHeight * imageRatio, rectHeight);