SDWebImageSVGNativeCoder
Background
Currently SDWebImage org provide 3 kinds of SVG Coder Plugin support, here is comparison:
Plugin Name | Vector Image | Bitmap Image | Platform | Compatibility | Dependency |
---|---|---|---|---|---|
SVGNativeCoder | NO | YES | iOS 9+ | Best and W3C standard | adobe/svg-native-viewer |
SVGCoder | YES | YES | iOS 13+ | OK but buggy on some SVG | Apple CoreSVG(Private) |
SVGKitPlugin | YES | NO | iOS 9+ | Worst, no longer maintain | SVGKit/SVGKit |
For now, I recommand to use this SVGNativeCoder (this repo) for most cases, until there are any other native support on Apple platforms.
SVG-Native
SVG Native is an upcoming specification of the SVG WG based on SVG OpenType.
SVG Native will be a strict subset of SVG 1.1 and SVG 2.0.
Requirements
- iOS 9+
- tvOS 9+
- macOS 10.11+
- watchOS 2+
Installation
CocoaPods
SDWebImageSVGNativeCoder is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SDWebImageSVGNativeCoder'
Carthage
SDWebImageSVGNativeCoder is available through Carthage.
github "SDWebImage/SDWebImageSVGNativeCoder"
Swift Package Manager
SDWebImageSVGNativeCoder is available through Swift Package Manager.
let package = Package(
dependencies: [
.package(url: "https://github.com/SDWebImage/SDWebImageSVGNativeCoder.git", from: "0.1")
]
)
Usage
Register Coder Plugin
To use SVG coder, you should firstly add the SDImageSVGNativeCoder
to the coders manager. Then you can call the View Category method to start load SVG images. See Wiki - Coder Usage here for these steps.
- Objective-C
// register coder, on AppDelegate
SDImageSVGNativeCoder *SVGCoder = [SDImageSVGNativeCoder sharedCoder];
[[SDImageCodersManager sharedManager] addCoder:SVGCoder];
- Swift
// register coder, on AppDelegate
let SVGCoder = SDImageSVGNativeCoder.shared
SDImageCodersManager.shared.addCoder(SVGCoder)
Render SVG as bitmap image
This coder plugin only support bitmap SVG image, which means once you load an image, even you change the image view size, the image size will not dynamic change and has to stretch up and may be blur. So you'd better provide the suitable large enough image size (like your image view size).
By default it use the SVG viewBox size. You can also specify a desired size during image loading using .imageThumbnailPixelSize
context option. And you can specify whether or not to keep aspect ratio during scale using .imagePreserveAspectRatio
context option.
- Objective-C
UIImageView *imageView;
CGSize bitmapSize = CGSizeMake(500, 500);
[imageView sd_setImageWithURL:url placeholderImage:nil options:0 context:@{SDWebImageContextThumbnailPixelSize: @(bitmapSize)];
- Swift
let imageView: UIImageView
let bitmapSize = CGSize(width: 500, height: 500)
imageView.sd_setImage(with: url, placeholderImage: nil, options: [], context: [.imageThumbnailPixelSize : bitmapSize])
Example
To run the example project, clone the repo, and run pod install
from the Example directory first.
Screenshot
Author
DreamPiggy
License
SDWebImageSVGNativeCoder is available under the MIT license. See the LICENSE file for more info.