Repository: software-mansion/react-native-svg (language: java)

Issue:
toDataURL incorrectly contains line breaks



# Bug

The `toDataURL` method contains line breaks `\r\n`, which break the base64 string.

#### Unexpected behavior
This can lead to corrupted images when e.g. using it in [react-native-share](https://github.com/react-native-share/react-native-share).

## Environment info


Library version: 13.4.0


## Short, Self Contained, Correct (Compilable), Example

https://snack.expo.dev/@robrechtme/data-url-bug




Discussion / hints:
Yes, i notice it too, and it happened only on IOS. i just remove it from base64 string:
```javascript
base64String.replace(/\r?\n|\r/g, '')
```
hope it will be fixed

Fix patch:
diff --git a/TestsExample/App.js b/TestsExample/App.js
index c5961c179..17bdd1266 100644
--- a/TestsExample/App.js
+++ b/TestsExample/App.js
@@ -12,6 +12,7 @@ import Test2071 from './src/Test2071';
 import Test2089 from './src/Test2089';
 import Test2196 from './src/Test2196';
 import Test2266 from './src/Test2266';
+import Test1986 from './src/Test1986';
 
 export default function App() {
   return <ColorTest />;
diff --git a/TestsExample/src/Test1986.tsx b/TestsExample/src/Test1986.tsx
new file mode 100644
index 000000000..b68458813
--- /dev/null
+++ b/TestsExample/src/Test1986.tsx
@@ -0,0 +1,29 @@
+import React, {useRef, useState} from 'react';
+import {Button, Image, View} from 'react-native';
+import {Circle, Svg} from 'react-native-svg';
+
+export default () => {
+  const ref = useRef<Svg>(null);
+  const [s, setS] = useState('');
+  return (
+    <View style={{flex: 1, paddingTop: 100}}>
+      <Button
+        onPress={() => {
+          ref.current?.toDataURL(source => {
+            setS(source);
+            console.log(source);
+          });
+        }}
+        title="log data url"
+      />
+      <Image
+        width={400}
+        height={300}
+        source={{uri: `data:image/png;base64,${s}`}}
+      />
+      <Svg width={400} height={300} ref={ref}>
+        <Circle cx={200} cy={150} r={100} fill="red" />
+      </Svg>
+    </View>
+  );
+};
diff --git a/android/src/main/java/com/horcrux/svg/SvgView.java b/android/src/main/java/com/horcrux/svg/SvgView.java
index bfbbe3323..dd50add28 100644
--- a/android/src/main/java/com/horcrux/svg/SvgView.java
+++ b/android/src/main/java/com/horcrux/svg/SvgView.java
@@ -339,7 +339,7 @@ String toDataURL() {
     bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
     bitmap.recycle();
     byte[] bitmapBytes = stream.toByteArray();
-    return Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
+    return Base64.encodeToString(bitmapBytes, Base64.NO_WRAP);
   }
 
   String toDataURL(int width, int height) {
@@ -353,7 +353,7 @@ String toDataURL(int width, int height) {
     bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
     bitmap.recycle();
     byte[] bitmapBytes = stream.toByteArray();
-    return Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
+    return Base64.encodeToString(bitmapBytes, Base64.NO_WRAP);
   }
 
   void enableTouchEvents() {
diff --git a/apple/Elements/RNSVGSvgView.mm b/apple/Elements/RNSVGSvgView.mm
index 3047b4b8a..7addf84e4 100644
--- a/apple/Elements/RNSVGSvgView.mm
+++ b/apple/Elements/RNSVGSvgView.mm
@@ -344,10 +344,10 @@ - (NSString *)getDataURLWithBounds:(CGRect)bounds
 #endif
 #if !TARGET_OS_OSX // [macOS]
   NSData *imageData = UIImagePNGRepresentation(image);
-  NSString *base64 = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
+  NSString *base64 = [imageData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
 #else // [macOS
   NSData *imageData = UIImagePNGRepresentation(UIGraphicsGetImageFromCurrentImageContext());
-  NSString *base64 = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
+  NSString *base64 = [imageData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
   UIGraphicsEndImageContext();
 #endif // macOS]
   return base64;


Test patch: