Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: implementation toDataURL for iOS platform both architectures #2405

Merged
merged 3 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 46 additions & 44 deletions apple/RNSVGSvgViewModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -26,54 +26,47 @@ - (void)toDataURL:(nonnull NSNumber *)reactTag
callback:(RCTResponseSenderBlock)callback
attempt:(int)attempt
{
void (^block)(void) = ^{
#ifdef RCT_NEW_ARCH_ENABLED
[self.viewRegistry_DEPRECATED addUIBlock:^(RCTViewRegistry *viewRegistry) {
__kindof RNSVGPlatformView *view = [viewRegistry viewForReactTag:reactTag];
[self.viewRegistry_DEPRECATED addUIBlock:^(RCTViewRegistry *viewRegistry) {
__kindof RNSVGPlatformView *view = [viewRegistry viewForReactTag:reactTag];
#else
[self.bridge.uiManager
addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *, RNSVGPlatformView *> *viewRegistry) {
__kindof RNSVGPlatformView *view = [uiManager viewForReactTag:reactTag];
[self.bridge.uiManager
addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *, RNSVGPlatformView *> *viewRegistry) {
__kindof RNSVGPlatformView *view = [uiManager viewForReactTag:reactTag];
#endif // RCT_NEW_ARCH_ENABLED
NSString *b64;
if ([view isKindOfClass:[RNSVGSvgView class]]) {
RNSVGSvgView *svg = view;
if (options == nil) {
b64 = [svg getDataURLWithBounds:svg.boundingBox];
} else {
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];

CGRect bounds = CGRectMake(0, 0, wi, hi);
b64 = [svg getDataURLWithBounds:bounds];
}
} else {
RCTLogError(@"Invalid svg returned from registry, expecting RNSVGSvgView, got: %@", view);
return;
}
if (b64) {
callback(@[ b64 ]);
} else if (attempt < 1) {
[self toDataURL:reactTag options:options callback:callback attempt:(attempt + 1)];
NSString *b64;
if ([view isKindOfClass:[RNSVGSvgView class]]) {
RNSVGSvgView *svg = view;
if (options == nil) {
b64 = [svg getDataURLWithBounds:svg.boundingBox];
} else {
callback(@[]);
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];

CGRect bounds = CGRectMake(0, 0, wi, hi);
b64 = [svg getDataURLWithBounds:bounds];
}
}];
};
if (self.bridge) {
dispatch_async(RCTGetUIManagerQueue(), block);
} else {
dispatch_async(dispatch_get_main_queue(), block);
}
}
} else {
RCTLogError(@"Invalid svg returned from registry, expecting RNSVGSvgView, got: %@", view);
return;
}
if (b64) {
callback(@[ b64 ]);
} else if (attempt < 1) {
[self toDataURL:reactTag options:options callback:callback attempt:(attempt + 1)];
} else {
callback(@[]);
}
}];
};

RCT_EXPORT_METHOD(toDataURL
: (nonnull NSNumber *)reactTag options
Expand All @@ -91,4 +84,13 @@ - (void)toDataURL:(nonnull NSNumber *)reactTag
}
#endif

@end
- (dispatch_queue_t)methodQueue
{
if (self.bridge) {
return RCTGetUIManagerQueue();
} else {
return dispatch_get_main_queue();
}
}

@end
jakex7 marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions apps/test-examples/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import Test2248 from './src/Test2248';
import Test2266 from './src/Test2266';
import Test2276 from './src/Test2276';
import Test2327 from './src/Test2327';
import Test2233 from './src/Test2233';
import Test2366 from './src/Test2366';

export default function App() {
Expand Down
30 changes: 30 additions & 0 deletions apps/test-examples/src/Test2233.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as React from 'react';
import {Button, SafeAreaView, View} from 'react-native';
import Svg, {Path} from 'react-native-svg';

const SvgLogoWelcome = () => {
const ref = React.useRef<Svg | null>(null);
return (
<View>
<Button
onPress={() => {
ref.current?.toDataURL(base64Image => {
console.log(base64Image, 'data');
});
}}
title="log"
/>
<Svg ref={ref} viewBox="0 0 24 24" fill="black">
<Path d="M12 2c5.514 0 10 4.486 10 10s-4.486 10-10 10S2 17.514 2 12 6.486 2 12 2zm0-2C5.373 0 0 5.373 0 12s5.373 12 12 12 12-5.373 12-12S18.627 0 12 0zm5.507 13.941c-1.512 1.195-3.174 1.931-5.506 1.931-2.334 0-3.996-.736-5.508-1.931L6 14.434C7.127 16.154 9.2 18 12.001 18c2.8 0 4.872-1.846 5.999-3.566l-.493-.493zM8.5 8a1.5 1.5 0 100 3 1.5 1.5 0 000-3zm7 0a1.5 1.5 0 100 3 1.5 1.5 0 000-3z" />
</Svg>
</View>
);
};

export default function App() {
return (
<SafeAreaView>
<SvgLogoWelcome />
</SafeAreaView>
);
}
Loading