Creating a UIImage from SVG string

2019-09-17 23:57发布

问题:

Hi I am trying to present an SVG image from data that I get from the server.

I am using this code with a library called SVGgh-

NSData *svgData =  [[NSData alloc] initWithBase64EncodedString:svgStr options:0];
NSString* newStr = [[NSString alloc] initWithData:svgData encoding:NSUTF8StringEncoding];
 NSString  *finalString = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">%@", newStr];
aRenderer = [[SVGRenderer alloc] initWithString:finalString];
UIImage *image = [aRenderer asImageWithSize:CGSizeMake(200, 200) andScale:1.0];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, 200.0, 200.0);
imageView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:imageView];

But all I get is a black view. Can someone direct me to what is going wrong ?

Here is the string I pass - Here is the data after :

 <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 259.6 312.79"><defs><style>.a,.e{font-size:18px;}.a,.c,.e,.f{font-family:Iowan Old Style;}.a,.c{font-style:italic;}.b,.d{fill:none;}.c{font-size:23px;}.d{stroke:#000;}.f{font-size:16px;}</style></defs><title>ACT-Math-7862-q</title><text class="a" transform="translate(70.83 217.15)">H</text><rect class="b" x="-435.5" y="-153.52" width="1024" height="768"/><text class="c" transform="translate(222.5 266.48)">x</text><rect class="b" x="222.15" y="241.56" width="9.06" height="25"/><polyline class="d" points="1 255.5 209.82 255.5 210.82 255.5"/><polygon points="209.82 260.3 219.42 255.5 209.82 250.7 209.82 260.3"/><rect class="b" y="249.7" width="220.42" height="11.6"/><text class="c" transform="translate(33.5 24.48)">y</text><rect class="b" x="25.85" width="25.74" height="30"/><polyline class="d" points="38.2 286.16 38.2 43.42 38.2 42.42"/><polygon points="43 43.42 38.2 33.82 33.4 43.42 43 43.42"/><rect class="b" x="-435.5" y="-153.52" width="1024" height="768"/><rect class="b" x="32.4" y="32.82" width="11.6" height="254.33"/><text class="e" transform="translate(190.5 216.48)">(8,2)</text><text class="e" transform="translate(62.5 106.48)">(1,5)</text><text class="a" transform="translate(52.5 106.48)">J</text><polygon class="d" points="214.68 116.69 181.75 199.81 92.68 199.72 62.12 116.39 214.68 116.69"/><text class="e" transform="translate(86.16 217.15)">(4,2)</text><text class="e" transform="translate(219.5 106.48)">(9,5)</text><text class="a" transform="translate(179.88 217.15)">L</text><text class="a" transform="translate(206.5 106.48)">K</text><rect class="b" x="-435.5" y="-153.52" width="1024" height="768"/><circle cx="214.68" cy="116.69" r="1.87"/><circle cx="62.12" cy="116.4" r="1.87"/><circle cx="92.68" cy="199.72" r="1.87"/><circle cx="181.75" cy="199.82" r="1.87"/><text class="f" transform="translate(22.5 305.48)">Note: <tspan x="44.2" y="0">F</tspan><tspan x="53.27" y="0">igure not drawn to scale.</tspan></text></svg>

Thanks Shani

标签: ios svg uiimage