PocketSVG

Easily convert your SVG files into CGPaths, CAShapeLayers, and UIBezierPaths

Github星跟蹤圖

PocketSVG

CocoaPods Build Status CocoaPods Compatible Carthage compatible Code Coverage code size license

A simple toolkit for displaying and manipulating SVGs on iOS and macOS in a performant manner.

The goal of this project is not to be a fully compliant SVG parser/renderer. But rather to use SVG as a format for serializing CG/UIPaths, meaning it only supports SVG features that can be represented by CG/UIPaths.

Thoroughly documented.

Features

  • Support for SVG elements: path, line, polyline, polygon, rect, circle, ellipse
  • Support for SVG named colors.
  • Fully working iOS and macOS demos.
  • Straightforward API for typical SVG rendering as a UIImageView/NSImageView or CALayer subclass.
  • Access every shape within your SVG as a CGPath for more fine-grained manipulation.

Installation

Cocoapods

Add this to your Podfile:

pod 'PocketSVG', '~> 2.0'

Then run pod install

Carthage

Add this to your Cartfile:

github "pocketsvg/PocketSVG" ~> 2.0

Then run carthage update

Manual

Drag and drop PocketSVG.xcodeproj into your Xcode project. In your project settings, add PocketSVG.framework in Link Binary With Libraries.

Usage

Render an SVG file using SVGImageView

let url = Bundle.main.url(forResource: "tiger", withExtension: "svg")!
let svgImageView = SVGImageView.init(contentsOf: url)
svgImageView.frame = view.bounds
svgImageView.contentMode = .scaleAspectFit
view.addSubview(svgImageView)

Output
image

Note: By default, SVGLayer has shouldRasterize set to YES when running on iOS. If you need to animate changes to the layer's transform you might want to reset that to NO.

Manually render each path of an SVG file using CAShapeLayer

view.backgroundColor = .white

let svgURL = Bundle.main.url(forResource: "tiger", withExtension: "svg")!
let paths = SVGBezierPath.pathsFromSVG(at: svgURL)
let tigerLayer = CALayer()
for (index, path) in paths.enumerated() {
    let shapeLayer = CAShapeLayer()
    shapeLayer.path = path.cgPath
    if index%2 == 0 {
        shapeLayer.fillColor = UIColor.black.cgColor
    }
    else if index%3 == 0 {
        shapeLayer.fillColor = UIColor.darkGray.cgColor
    }
    else {
        shapeLayer.fillColor = UIColor.gray.cgColor
    }
    tigerLayer.addSublayer(shapeLayer)
}

var transform = CATransform3DMakeScale(0.4, 0.4, 1.0)
transform = CATransform3DTranslate(transform, 200, 400, 0)
tigerLayer.transform = transform
view.layer.addSublayer(tigerLayer)

Output
image

Contributing

Bug Reports & Feature Requests

Please use the issue tracker to report any bugs or file feature requests.

Developing

PRs are welcome.

主要指標

概覽
名稱與所有者pocketsvg/PocketSVG
主編程語言Objective-C++
編程語言Objective-C (語言數: 5)
平台
許可證MIT License
所有者活动
創建於2012-08-02 11:27:10
推送於2025-02-07 13:48:47
最后一次提交2025-02-07 14:46:21
發布數22
最新版本名稱2.8.0 (發布於 )
第一版名稱0.4 (發布於 )
用户参与
星數1.7k
關注者數37
派生數246
提交數429
已啟用問題?
問題數138
打開的問題數8
拉請求數66
打開的拉請求數1
關閉的拉請求數20
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?