根据上司要求把所有的图片都改成SVG格式的,然后我就在网上找了一些类库,比如说SVGKit类库,网上也有好多它的使用,但是我用的swift,SVGKit是OC的库,这时也有人会说SVGKit也可以在swift项目中使用,但是我搞了一天没成功,可能是我太笨了,然后我就自己写了一个根据路径串在画板上把这张图画出来,话不多说,直接上代码。
ViewController类中创建一个ImageView,将画出的svgImage放到ImageView上
import UIKit
class ViewController: UIViewController {
var imageView : UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.\
imageView = UIImageView(frame: CGRect(x: 40, y: 70, width: 250, height: 250))
self.view.addSubview(imageView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func onClick(_ sender:Any) {
let image =SVGImage().convertViewToSVGImage(width:imageView.bounds.width,height:imageView.bounds.height,data:"M2,21.625 C2,21.625 30,21.625 30,21.625 31.105,21.625 32,22.52 32,23.625 32,23.625 32,25 32,25 32,26.105 31.105,27 30,27 30,27 2,27 2,27 0.89499998,27 0,26.105 0,25 0,25 0,23.625 0,23.625 0,22.52 0.89499998,21.625 2,21.625 z M2,11.125 C2,11.125 30,11.125 30,11.125 31.105,11.125 32,12.02 32,13.125 32,13.125 32,14.5 32,14.5 32,15.605 31.105,16.5 30,16.5 30,16.5 2,16.5 2,16.5 0.89499998,16.5 0,15.605 0,14.5 0,14.5 0,13.125 0,13.125 0,12.02 0.89499998,11.125 2,11.125 z M2,0 C2,0 30,0 30,0 31.105,0 32,0.89499998 32,2 32,2 32,3.375 32,3.375 32,4.48 31.105,5.375 30,5.375 30,5.375 2,5.375 2,5.375 0.89499998,5.375 0,4.48 0,3.375 0,3.375 0,2 0,2 0,0.89499998 0.89499998,0 2,0 z", lineColor:UIColor.red,fillColor:UIColor,blue)
imageView.image = image
}
}
SVGImage类将DrawSvgPathView画出来的图转成Image
import UIKit
open class SVGImage:NSObject {
let svgView : DrawSvgPathView!
public override init() {
// super.init()
svgView = DrawSvgPathView.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
}
//将视图转成Image
open func convertViewToSVGImage(width:CGFloat,height:CGFloat,data:String, lineColor:UIColor, fillColor: UIColor? = UIColor.clear) ->UIImage?{
svgView.layer.sublayers =nil
svgView.setPathFromSvg(width: width,height: height,data: data, lineColor: lineColor, fillColor: fillColor)
UIGraphicsBeginImageContextWithOptions(CGSize(width:width,height:height),false,UIScreen.main.scale)
svgView.layer.render(in:UIGraphicsGetCurrentContext()!)
let image =UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
DrawSvgPathView类根据PocketSVG解析出来的路径绘画图片。
import UIKit
class DrawSvgPathView: UIView {
var _shapeView :CAShapeLayer =CAShapeLayer()
var _strokeLineColor : UIColor!
var _strokeFillColor : UIColor!
override init(frame:CGRect) {
super.init(frame: frame)
}
required publicinit?(coder aDecoder:NSCoder) {
fatalError("init(coder:) has not been implemented")
}
open func setPathFromSvg(width:CGFloat,height:CGFloat,data:String, lineColor:UIColor, fillColor: UIColor? = UIColor.clear){
self.layer.sublayers =nil
self.bounds.size =CGSize(width: width, height: height)
self._strokeLineColor = lineColor
self._strokeFillColor = fillColor
let pocketSVG = PocketSVG(str: data)
self._shapeView.path = pocketSVG.bezier.cgPath
self.scale()
}
func scale(){ </