0

I've imported SVGKit lib into my project and I would like to load svg image that I have stored in NSString like that:

<svg xmlns="http://www.w3.org/2000/svg" ....
</svg>

When I try to load it via

SVGKImage* newImage = [SVGKImage imageWithContentsOfURL:svgUrl];

I get err because the url is nil, thats obvious because it isn't URL, but is there any method in library that I can use??

Skodik.o
  • 506
  • 4
  • 21

3 Answers3

0

You need to create a NSURL object from the string representation of the URL, so:

NSString *strURL = "http://......";

NSURL *imageURL = [[NSURL alloc] initWithString:strURL];

SVGKImage *svgImage = [SVGKImage imageWithContentsOfURL:imageURL];

Does that help?

Zhang
  • 11,549
  • 7
  • 57
  • 87
  • I know this approach, but I have only string representation of SVG itself not the url ... :( – Skodik.o Jun 10 '14 at 08:46
  • Oh right, so you've got a SVG as an XML that you need to parse? I found this: http://t-machine.org/index.php/2012/12/31/svgkit-2013-usage/ which talks a bit about it. – Zhang Jun 10 '14 at 08:53
0

You can do this:

NSData* svgData = [svgSource dataUsingEncoding: NSUTF8StringEncoding];
SVGKImage *svgImage = [SVGKImage imageWithData:svgData];

However, I suspect that what you should be doing is what Zhang is suggesting. In any case, you should make sure that when you download the data you do it off the main thread.

ThomasW
  • 16,981
  • 4
  • 79
  • 106
  • I would like to use this approach, but I get Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Method unsupported / not yet implemented by SVGKit' so this is dead end :( – Skodik.o Jun 10 '14 at 08:43
0

Using the SVGKit 2.x, you can use the SVGKLayeredImageView and load the image from URL: (My sample code in Swift Xcode 7.1)

  1. In Interface Builder, drag a "View" into the your Scene.
  2. Go to Identity Inspector and change that Custom Class of that view is SVGKLayeredImageView (If the class is not available and you do not see it here, that means you do not import SVGKit the same version I have or do not install the framework properly)

  3. Make outlet connection of that view then you should have

@IBOutlet weak var svgImageView: SVGKLayeredImageView!

  1. Usage

      if let url = NSURL(string: "http://your-svg-file-url"){
            if url.pathExtension!.lowercaseString == "svg" {
    
                svgImageView.image = SVGKImage(contentsOfURL: url)
    
            }
        }
    
Telvin Nguyen
  • 3,569
  • 4
  • 25
  • 39