I have a .svg image file I want to display in my project.
我想在我的项目中显示一个.svg图像文件。
I tried using UIImageView, which works for the .png & .jpg image formats, but not for the .svg extension. Is there any way to display a .svg image using UIWebView or UIImageView ?
我尝试过使用UIImageView,它适用于.png和.jpg图像格式,但不适用于.svg扩展。是否有办法使用UIWebView或UIImageView显示.svg图像?
8 个解决方案
#1
6
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
#2
9
You can use SVGKit for example.
例如,您可以使用SVGKit。
1) Integrate it according to instructions. Drag&dropping the .framework file is fast and easy.
1)按照说明进行集成。拖放.framework文件是快速和容易的。
2) Make sure you have an Objective-C to Swift bridge file bridging-header.h with import code in it:
2)确保您有一个Objective-C到Swift桥接文件布里奇头。h,里面有导入代码:
#import <SVGKit/SVGKit.h>
#import <SVGKit/SVGKImage.h>
3) Use the framework like this, assuming that dataFromInternet is NSData, previously downloaded from network:
3)使用这样的框架,假设dataFromInternet是NSData,之前是从网络下载的:
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.
该框架还允许从其他不同来源初始化一个SVGKImage,例如,当您提供URL时,它可以为您下载图像。但在我的例子中,它在无法访问url的情况下崩溃了,所以最好是自己管理网络。这里有更多信息。
#3
7
There is no Inbuilt support for SVG in Swift. So we need to use other libraries.
在Swift中不支持SVG。所以我们需要使用其他库。
The simple SVG libraries in swift are :
swift中的简单SVG库是:
1) SwiftSVG Library
1)SwiftSVG图书馆
It gives you more option to Import as UIView, CAShapeLayer, Path, etc
它提供了更多的导入选项,如UIView、CAShapeLayer、Path等等
To modify your SVG Color and Import as UIImage you can use my extension codes for the library mentioned in below link,
要修改SVG颜色并将其作为UIImage导入,您可以使用下面链接中提到的库的扩展代码,
Click here to know on using SwiftSVG library :
Using SwiftSVG to set SVG for Image
单击这里了解使用SwiftSVG库:使用SwiftSVG为图像设置SVG
|OR|
|和|
2) SVGKit Library
2)SVGKit图书馆
2.1) Use pod to install :
2.1)使用pod安装:
pod 'SVGKit', :git => 'https://github.com/SVGKit/SVGKit.git', :branch => '2.x'
2.2) Add framework
2.2)添加框架
Goto AppSettings
-> General Tab
-> Scroll down to Linked Frameworks and Libraries
-> Click on plus icon
-> Select SVG.frameworkGoto AppSettings -> General选项卡->向下滚动到链接的框架和库->单击plus图标->选择SVG.framework
2.3) Add in Objective-C to Swift bridge file bridging-header.h :
2.3)将Objective-C添加到Swift网桥文件布里奇头中。h:
#import <SVGKit/SVGKit.h>
#import <SVGKit/SVGKImage.h>
2.4) Create SvgImg Folder (for better organization) in Project and add SVG files inside it.
2.4)在项目中创建SvgImg文件夹(为了更好地组织),并在其中添加SVG文件。
Note : Adding Inside Assets Folder won't work and SVGKit searches for file only in Project folders
注:添加Inside Assets文件夹不会工作,SVGKit只在项目文件夹中搜索文件。
2.5) Use in your Swift Code as below :
2.5)在您的Swift代码中使用如下:
import SVGKit
and
和
let namSvgImgVar: SVGKImage = SVGKImage(named: "NamSvgImj")
Note : SVGKit Automatically apends extention ".svg" to the string you specify
注意:SVGKit自动分配扩展。svg“用于指定的字符串
let namSvgImgVyuVar = SVGKImageView(SVGKImage: namSvgImgVar)
let namImjVar: UIImage = namSvgImgVar.UIImage
There are many more options for you to init SVGKImage and SVGKImageView
init SVGKImage和SVGKImageView还有很多选项
There are also other classes u can explore
你还可以探索其他的类
SVGRect
SVGCurve
SVGPoint
SVGAngle
SVGColor
SVGLength
and etc ...
#4
5
Swift 3.0 version :
let path = Bundle.main.path(forResource: "svgNameFileHere", ofType: "svg")!
if path != "" {
let fileURL:URL = URL(fileURLWithPath: path)
let req = URLRequest(url: fileURL)
self.webView.scalesPageToFit = false
self.webView.loadRequest(req)
}
else {
//handle here if path not found
}
#5
2
You can keep your images as strings and use WKWebView to display them:
您可以将您的图像保存为字符串,并使用WKWebView来显示它们:
let webView: WKWebView = {
let mySVGImage = "<svg height=\"190\"><polygon points=\"100,10 40,180 190,60 10,60 160,180\" style=\"fill:lime;stroke:purple;stroke-width:5;fill-rule:evenodd;\"></svg>"
let preferences = WKPreferences()
preferences.javaScriptEnabled = false
let configuration = WKWebViewConfiguration()
configuration.preferences = preferences
let wv = WKWebView(frame: .zero, configuration: configuration)
wv.scrollView.isScrollEnabled = false
wv.loadHTMLString(mySVGImage, baseURL: nil)
return wv
}()
#6
2
Here's a simple class that can display SVG images in a UIView
这里有一个简单的类,可以在UIView中显示SVG图像。
import UIKit
public class SVGImageView: UIView {
private let webView = UIWebView()
public init() {
super.init(frame: .zero)
webView.delegate = self
webView.scrollView.isScrollEnabled = false
webView.contentMode = .scaleAspectFit
webView.backgroundColor = .clear
addSubview(webView)
webView.snp.makeConstraints { make in
make.edges.equalTo(self)
}
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
webView.stopLoading()
}
public func load(url: String) {
webView.stopLoading()
if let url = URL(string: fullUrl) {
webView.loadRequest(URLRequest(url: url))
}
}
}
extension SVGImageView: UIWebViewDelegate {
public func webViewDidFinishLoad(_ webView: UIWebView) {
let scaleFactor = webView.bounds.size.width / webView.scrollView.contentSize.width
if scaleFactor <= 0 {
return
}
webView.scrollView.minimumZoomScale = scaleFactor
webView.scrollView.maximumZoomScale = scaleFactor
webView.scrollView.zoomScale = scaleFactor
}
}
#7
1
In case you want to use a WKWebView
to load a .svg image that is coming from a URLRequest
, you can simply achieve it like this:
如果您希望使用WKWebView来加载来自URLRequest的.svg图像,您可以简单地实现如下操作:
Swift 4
斯威夫特4
if let request = URLRequest(url: urlString), let svgString = try? String(contentsOf: request) {
wkWebView.loadHTMLString(svgString, baseURL: request)
}
It's much simpler than the other ways of doing it, and you can also persist your .svg string somewhere to load it later, even offline if you need to.
它比其他方法简单得多,而且您还可以将.svg字符串保存在某个地方,以便稍后加载它,甚至在需要时也可以离线。
#8
0
To render SVG file you can use Macaw. Also Macaw supports transformations, user events, animation and various effects.
要呈现SVG文件,可以使用Macaw。Macaw还支持转换、用户事件、动画和各种效果。
You can render SVG file with zero lines of code. For more info please check this article: Render SVG file with Macaw.
您可以用零行代码呈现SVG文件。有关更多信息,请查看本文:使用Macaw呈现SVG文件。
#1
6
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
#2
9
You can use SVGKit for example.
例如,您可以使用SVGKit。
1) Integrate it according to instructions. Drag&dropping the .framework file is fast and easy.
1)按照说明进行集成。拖放.framework文件是快速和容易的。
2) Make sure you have an Objective-C to Swift bridge file bridging-header.h with import code in it:
2)确保您有一个Objective-C到Swift桥接文件布里奇头。h,里面有导入代码:
#import <SVGKit/SVGKit.h>
#import <SVGKit/SVGKImage.h>
3) Use the framework like this, assuming that dataFromInternet is NSData, previously downloaded from network:
3)使用这样的框架,假设dataFromInternet是NSData,之前是从网络下载的:
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.
该框架还允许从其他不同来源初始化一个SVGKImage,例如,当您提供URL时,它可以为您下载图像。但在我的例子中,它在无法访问url的情况下崩溃了,所以最好是自己管理网络。这里有更多信息。
#3
7
There is no Inbuilt support for SVG in Swift. So we need to use other libraries.
在Swift中不支持SVG。所以我们需要使用其他库。
The simple SVG libraries in swift are :
swift中的简单SVG库是:
1) SwiftSVG Library
1)SwiftSVG图书馆
It gives you more option to Import as UIView, CAShapeLayer, Path, etc
它提供了更多的导入选项,如UIView、CAShapeLayer、Path等等
To modify your SVG Color and Import as UIImage you can use my extension codes for the library mentioned in below link,
要修改SVG颜色并将其作为UIImage导入,您可以使用下面链接中提到的库的扩展代码,
Click here to know on using SwiftSVG library :
Using SwiftSVG to set SVG for Image
单击这里了解使用SwiftSVG库:使用SwiftSVG为图像设置SVG
|OR|
|和|
2) SVGKit Library
2)SVGKit图书馆
2.1) Use pod to install :
2.1)使用pod安装:
pod 'SVGKit', :git => 'https://github.com/SVGKit/SVGKit.git', :branch => '2.x'
2.2) Add framework
2.2)添加框架
Goto AppSettings
-> General Tab
-> Scroll down to Linked Frameworks and Libraries
-> Click on plus icon
-> Select SVG.frameworkGoto AppSettings -> General选项卡->向下滚动到链接的框架和库->单击plus图标->选择SVG.framework
2.3) Add in Objective-C to Swift bridge file bridging-header.h :
2.3)将Objective-C添加到Swift网桥文件布里奇头中。h:
#import <SVGKit/SVGKit.h>
#import <SVGKit/SVGKImage.h>
2.4) Create SvgImg Folder (for better organization) in Project and add SVG files inside it.
2.4)在项目中创建SvgImg文件夹(为了更好地组织),并在其中添加SVG文件。
Note : Adding Inside Assets Folder won't work and SVGKit searches for file only in Project folders
注:添加Inside Assets文件夹不会工作,SVGKit只在项目文件夹中搜索文件。
2.5) Use in your Swift Code as below :
2.5)在您的Swift代码中使用如下:
import SVGKit
and
和
let namSvgImgVar: SVGKImage = SVGKImage(named: "NamSvgImj")
Note : SVGKit Automatically apends extention ".svg" to the string you specify
注意:SVGKit自动分配扩展。svg“用于指定的字符串
let namSvgImgVyuVar = SVGKImageView(SVGKImage: namSvgImgVar)
let namImjVar: UIImage = namSvgImgVar.UIImage
There are many more options for you to init SVGKImage and SVGKImageView
init SVGKImage和SVGKImageView还有很多选项
There are also other classes u can explore
你还可以探索其他的类
SVGRect
SVGCurve
SVGPoint
SVGAngle
SVGColor
SVGLength
and etc ...
#4
5
Swift 3.0 version :
let path = Bundle.main.path(forResource: "svgNameFileHere", ofType: "svg")!
if path != "" {
let fileURL:URL = URL(fileURLWithPath: path)
let req = URLRequest(url: fileURL)
self.webView.scalesPageToFit = false
self.webView.loadRequest(req)
}
else {
//handle here if path not found
}
#5
2
You can keep your images as strings and use WKWebView to display them:
您可以将您的图像保存为字符串,并使用WKWebView来显示它们:
let webView: WKWebView = {
let mySVGImage = "<svg height=\"190\"><polygon points=\"100,10 40,180 190,60 10,60 160,180\" style=\"fill:lime;stroke:purple;stroke-width:5;fill-rule:evenodd;\"></svg>"
let preferences = WKPreferences()
preferences.javaScriptEnabled = false
let configuration = WKWebViewConfiguration()
configuration.preferences = preferences
let wv = WKWebView(frame: .zero, configuration: configuration)
wv.scrollView.isScrollEnabled = false
wv.loadHTMLString(mySVGImage, baseURL: nil)
return wv
}()
#6
2
Here's a simple class that can display SVG images in a UIView
这里有一个简单的类,可以在UIView中显示SVG图像。
import UIKit
public class SVGImageView: UIView {
private let webView = UIWebView()
public init() {
super.init(frame: .zero)
webView.delegate = self
webView.scrollView.isScrollEnabled = false
webView.contentMode = .scaleAspectFit
webView.backgroundColor = .clear
addSubview(webView)
webView.snp.makeConstraints { make in
make.edges.equalTo(self)
}
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
webView.stopLoading()
}
public func load(url: String) {
webView.stopLoading()
if let url = URL(string: fullUrl) {
webView.loadRequest(URLRequest(url: url))
}
}
}
extension SVGImageView: UIWebViewDelegate {
public func webViewDidFinishLoad(_ webView: UIWebView) {
let scaleFactor = webView.bounds.size.width / webView.scrollView.contentSize.width
if scaleFactor <= 0 {
return
}
webView.scrollView.minimumZoomScale = scaleFactor
webView.scrollView.maximumZoomScale = scaleFactor
webView.scrollView.zoomScale = scaleFactor
}
}
#7
1
In case you want to use a WKWebView
to load a .svg image that is coming from a URLRequest
, you can simply achieve it like this:
如果您希望使用WKWebView来加载来自URLRequest的.svg图像,您可以简单地实现如下操作:
Swift 4
斯威夫特4
if let request = URLRequest(url: urlString), let svgString = try? String(contentsOf: request) {
wkWebView.loadHTMLString(svgString, baseURL: request)
}
It's much simpler than the other ways of doing it, and you can also persist your .svg string somewhere to load it later, even offline if you need to.
它比其他方法简单得多,而且您还可以将.svg字符串保存在某个地方,以便稍后加载它,甚至在需要时也可以离线。
#8
0
To render SVG file you can use Macaw. Also Macaw supports transformations, user events, animation and various effects.
要呈现SVG文件,可以使用Macaw。Macaw还支持转换、用户事件、动画和各种效果。
You can render SVG file with zero lines of code. For more info please check this article: Render SVG file with Macaw.
您可以用零行代码呈现SVG文件。有关更多信息,请查看本文:使用Macaw呈现SVG文件。