mirror of
https://github.com/zoriya/react-native-svg.git
synced 2026-06-05 16:04:38 +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);
|
||||
}
|
||||
|
||||
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() {
|
||||
if (!mResponsible) {
|
||||
mResponsible = true;
|
||||
|
||||
@@ -13,6 +13,7 @@ import com.facebook.react.bridge.Callback;
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
||||
import com.facebook.react.bridge.ReactMethod;
|
||||
import com.facebook.react.bridge.ReadableMap;
|
||||
|
||||
class SvgViewModule extends ReactContextBaseJavaModule {
|
||||
SvgViewModule(ReactApplicationContext reactContext) {
|
||||
@@ -33,4 +34,19 @@ class SvgViewModule extends ReactContextBaseJavaModule {
|
||||
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")
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user