mirror of
https://github.com/zoriya/react-native-svg.git
synced 2026-06-06 08:22:23 +00:00
Implement version of toDataURL with width and height options
https://github.com/react-native-community/react-native-svg/issues/855
This commit is contained in:
@@ -306,6 +306,20 @@ public class SvgView extends ReactViewGroup implements ReactCompoundView, ReactC
|
|||||||
return Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
|
return Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String toDataURL(int width, int height) {
|
||||||
|
Bitmap bitmap = Bitmap.createBitmap(
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
Bitmap.Config.ARGB_8888);
|
||||||
|
|
||||||
|
drawChildren(new Canvas(bitmap));
|
||||||
|
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||||
|
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
|
||||||
|
bitmap.recycle();
|
||||||
|
byte[] bitmapBytes = stream.toByteArray();
|
||||||
|
return Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
|
||||||
|
}
|
||||||
|
|
||||||
void enableTouchEvents() {
|
void enableTouchEvents() {
|
||||||
if (!mResponsible) {
|
if (!mResponsible) {
|
||||||
mResponsible = true;
|
mResponsible = true;
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import com.facebook.react.bridge.Callback;
|
|||||||
import com.facebook.react.bridge.ReactApplicationContext;
|
import com.facebook.react.bridge.ReactApplicationContext;
|
||||||
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
||||||
import com.facebook.react.bridge.ReactMethod;
|
import com.facebook.react.bridge.ReactMethod;
|
||||||
|
import com.facebook.react.bridge.ReadableMap;
|
||||||
|
|
||||||
class SvgViewModule extends ReactContextBaseJavaModule {
|
class SvgViewModule extends ReactContextBaseJavaModule {
|
||||||
SvgViewModule(ReactApplicationContext reactContext) {
|
SvgViewModule(ReactApplicationContext reactContext) {
|
||||||
@@ -33,4 +34,19 @@ class SvgViewModule extends ReactContextBaseJavaModule {
|
|||||||
successCallback.invoke(svg.toDataURL());
|
successCallback.invoke(svg.toDataURL());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ReactMethod
|
||||||
|
public void toDataURL(int tag, ReadableMap options, Callback successCallback) {
|
||||||
|
SvgView svg = SvgViewManager.getSvgViewByTag(tag);
|
||||||
|
|
||||||
|
if (svg != null) {
|
||||||
|
successCallback.invoke(
|
||||||
|
svg.toDataURL(
|
||||||
|
options.getInt("width"),
|
||||||
|
options.getInt("height")
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-3
@@ -49,9 +49,16 @@ export default class Svg extends Shape {
|
|||||||
this.root.setNativeProps(props);
|
this.root.setNativeProps(props);
|
||||||
};
|
};
|
||||||
|
|
||||||
toDataURL = callback => {
|
toDataURL = (callback, options) => {
|
||||||
callback &&
|
if (!callback) {
|
||||||
RNSVGSvgViewManager.toDataURL(findNodeHandle(this.root), callback);
|
return;
|
||||||
|
}
|
||||||
|
const handle = findNodeHandle(this.root);
|
||||||
|
if (options) {
|
||||||
|
RNSVGSvgViewManager.toDataURL(handle, options, callback);
|
||||||
|
} else {
|
||||||
|
RNSVGSvgViewManager.toDataURL(handle, callback);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
|||||||
@@ -52,6 +52,8 @@
|
|||||||
|
|
||||||
- (NSString *)getDataURL;
|
- (NSString *)getDataURL;
|
||||||
|
|
||||||
|
- (NSString *)getDataURLwithBounds:(CGSize)bounds;
|
||||||
|
|
||||||
- (CGRect)getContextBounds;
|
- (CGRect)getContextBounds;
|
||||||
|
|
||||||
- (void)drawRect:(CGRect)rect;
|
- (void)drawRect:(CGRect)rect;
|
||||||
|
|||||||
@@ -247,7 +247,6 @@
|
|||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
- (NSString *)getDataURL
|
- (NSString *)getDataURL
|
||||||
{
|
{
|
||||||
UIGraphicsBeginImageContextWithOptions(_boundingBox.size, NO, 0);
|
UIGraphicsBeginImageContextWithOptions(_boundingBox.size, NO, 0);
|
||||||
@@ -258,6 +257,16 @@
|
|||||||
return base64;
|
return base64;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (NSString *)getDataURLwithBounds:(CGSize)bounds
|
||||||
|
{
|
||||||
|
UIGraphicsBeginImageContextWithOptions(bounds, NO, 0);
|
||||||
|
[self drawRect:_boundingBox];
|
||||||
|
NSData *imageData = UIImagePNGRepresentation(UIGraphicsGetImageFromCurrentImageContext());
|
||||||
|
NSString *base64 = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
|
||||||
|
UIGraphicsEndImageContext();
|
||||||
|
return base64;
|
||||||
|
}
|
||||||
|
|
||||||
- (void)reactSetInheritedBackgroundColor:(UIColor *)inheritedBackgroundColor
|
- (void)reactSetInheritedBackgroundColor:(UIColor *)inheritedBackgroundColor
|
||||||
{
|
{
|
||||||
self.backgroundColor = inheritedBackgroundColor;
|
self.backgroundColor = inheritedBackgroundColor;
|
||||||
|
|||||||
@@ -43,4 +43,30 @@ RCT_EXPORT_METHOD(toDataURL:(nonnull NSNumber *)reactTag callback:(RCTResponseSe
|
|||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RCT_EXPORT_METHOD(toDataURL:(nonnull NSNumber *)reactTag options:(NSDictionary *)options callback:(RCTResponseSenderBlock)callback)
|
||||||
|
{
|
||||||
|
[self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *,UIView *> *viewRegistry) {
|
||||||
|
__kindof UIView *view = viewRegistry[reactTag];
|
||||||
|
if ([view isKindOfClass:[RNSVGSvgView class]]) {
|
||||||
|
RNSVGSvgView *svg = view;
|
||||||
|
id width = [options objectForKey:@"width"];
|
||||||
|
id height = [options objectForKey:@"height"];
|
||||||
|
if (![width isKindOfClass:NSNumber.class] ||
|
||||||
|
![height isKindOfClass:NSNumber.class]) {
|
||||||
|
RCTLogError(@"Invalid width or height given to toDataURL");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
NSNumber* w = width;
|
||||||
|
NSInteger wi = (NSInteger)[w intValue];
|
||||||
|
NSNumber* h = height;
|
||||||
|
NSInteger hi = (NSInteger)[h intValue];
|
||||||
|
|
||||||
|
CGSize bounds = CGSizeMake(wi, hi);
|
||||||
|
callback(@[[svg getDataURLwithBounds:bounds]]);
|
||||||
|
} else {
|
||||||
|
RCTLogError(@"Invalid svg returned frin registry, expecting RNSVGSvgView, got: %@", view);
|
||||||
|
}
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
Reference in New Issue
Block a user