How to display .svg image using swift

前端 未结 13 831
你的背包
你的背包 2020-11-30 00:37

I have a .svg image file I want to display in my project.

I tried using UIImageView, which works for the .png & .jpg image formats, but not for the .svg extensi

相关标签:
13条回答
  • 2020-11-30 00:50

    As I know there are 2 different graphic formats:

    1. Raster graphics (uses bitmaps) and is used in JPEG, PNG, APNG, GIF, and MPEG4 file format.
    2. Vector graphics (uses points, lines, curves and other shapes). Vector graphics are used in the SVG, EPS, PDF or AI graphic file formats.

    So if you need to use an image stored in SVG File in your Xcode I would suggest:

    1. Convert SVG file to PDF. I used https://document.online-convert.com/convert/svg-to-pdf

    2. Use Xcode to manage you PDF file.

    0 讨论(0)
  • 2020-11-30 00:59

    Try this code

    var path: String = NSBundle.mainBundle().pathForResource("nameOfFile", ofType: "svg")!
    
            var url: NSURL = NSURL.fileURLWithPath(path)  //Creating a URL which points towards our path
    
           //Creating a page request which will load our URL (Which points to our path)
            var request: NSURLRequest = NSURLRequest(URL: url)
           webView.loadRequest(request)  //Telling our webView to load our above request
    
    0 讨论(0)
  • 2020-11-30 01:00

    You can use this pod called 'SVGParser'. https://cocoapods.org/pods/SVGParser.

    After adding it in your pod file, all you have to do is to import this module to the class that you want to use it. You should show the SVG image in an ImageView.

    There are three cases you can show this SVGimage:

    1. Load SVG from local path as Data
    2. Load SVG from local path
    3. Load SVG from remote URL

    You can also find an example project in GitHub: https://github.com/AndreyMomot/SVGParser. Just download the project and run it to see how it works.

    0 讨论(0)
  • To render SVG file you can use Macaw. Also Macaw supports transformations, user events, animation and various effects.

    You can render SVG file with zero lines of code. For more info please check this article: Render SVG file with Macaw.

    DISCLAIMER: I am affiliated with this project.

    0 讨论(0)
  • 2020-11-30 01:08

    SVG file support is added to Xcode 12.

    0 讨论(0)
  • 2020-11-30 01:09

    You can use SVGKit for example.

    1) Integrate it according to instructions. Drag&dropping the .framework file is fast and easy.

    2) Make sure you have an Objective-C to Swift bridge file bridging-header.h with import code in it:

    #import <SVGKit/SVGKit.h>
    #import <SVGKit/SVGKImage.h>
    

    3) Use the framework like this, assuming that dataFromInternet is NSData, previously downloaded from network:

    let anSVGImage: SVGKImage = SVGKImage(data: dataFromInternet)
    myIUImageView.image = anSVGImage.UIImage
    

    The framework also allows to init an SVGKImage from other different sources, for example it can download image for you when you provide it with URL. But in my case it was crashing in case of unreachable url, so it turned out to be better to manage networking by myself. More info on it here.

    0 讨论(0)
提交回复
热议问题