Skip to content

Commit

Permalink
fix: toDataUrl line breaks (#2272)
Browse files Browse the repository at this point in the history
# 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 |    ✅     |
  • Loading branch information
jakex7 authored May 15, 2024
1 parent 38a8dbc commit 118a20c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions TestsExample/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 />;
Expand Down
29 changes: 29 additions & 0 deletions TestsExample/src/Test1986.tsx
Original file line number Diff line number Diff line change
@@ -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>
);
};
4 changes: 2 additions & 2 deletions android/src/main/java/com/horcrux/svg/SvgView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions apple/Elements/RNSVGSvgView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 118a20c

Please sign in to comment.