How to display .svg image using swift

前端 未结 13 870
你的背包
你的背包 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 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 
    #import 
    

    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.

提交回复
热议问题