From 118a20c0fd9d89883fc6080b8b36a369148a4c89 Mon Sep 17 00:00:00 2001 From: Jakub Grzywacz Date: Wed, 15 May 2024 12:01:33 +0200 Subject: [PATCH] fix: toDataUrl line breaks (#2272) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Summary This PR removes the flag to include custom line breaks on `toDataUrl` method. Some software could not read base64 with additional line break characters, so it could lead to corrupting the image (like in react-native-share). Fixes #1986. ## Test Plan `TestsExample` -> `Test1986` ## Compatibility | OS | Implemented | | ------- | :---------: | | iOS | ✅ | | Android | ✅ | --- TestsExample/App.js | 1 + TestsExample/src/Test1986.tsx | 29 +++++++++++++++++++ .../main/java/com/horcrux/svg/SvgView.java | 4 +-- apple/Elements/RNSVGSvgView.mm | 4 +-- 4 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 TestsExample/src/Test1986.tsx diff --git a/TestsExample/App.js b/TestsExample/App.js index c5961c17..17bdd126 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 ; diff --git a/TestsExample/src/Test1986.tsx b/TestsExample/src/Test1986.tsx new file mode 100644 index 00000000..b6845881 --- /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(null); + const [s, setS] = useState(''); + return ( + + + ); +}; diff --git a/android/src/main/java/com/horcrux/svg/SvgView.java b/android/src/main/java/com/horcrux/svg/SvgView.java index bfbbe332..dd50add2 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 @@ public class SvgView extends ReactViewGroup implements ReactCompoundView, ReactC 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 @@ public class SvgView extends ReactViewGroup implements ReactCompoundView, ReactC 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 3047b4b8..7addf84e 100644 --- a/apple/Elements/RNSVGSvgView.mm +++ b/apple/Elements/RNSVGSvgView.mm @@ -344,10 +344,10 @@ using namespace facebook::react; #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;