Skip to content

SDWebImage/SDWebImageSVGNativeCoder

Repository files navigation

SDWebImageSVGNativeCoder

CI StatusVersionLicensePlatform

Background

Currently SDWebImage org provide 3 kinds of SVG Coder Plugin support, here is comparison:

Plugin NameVector ImageBitmap ImagePlatformCompatibilityDependency
SVGNativeCoderNOYESiOS 9+Best and W3C standardadobe/svg-native-viewer
SVGCoderYESYESiOS 13+OK but buggy on some SVGApple CoreSVG(Private)
SVGKitPluginYESNOiOS 9+Worst, no longer maintainSVGKit/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'

Note:

It's strongly recommended to use CocoaPods v1.7+ with multiple Xcode Projects, which can avoid issues when different header file contains the same file name. This applys to all SDWebImage organization maintained repo.

install!'cocoapods',generate_multiple_pod_projects: true

Carthage

SDWebImageSVGNativeCoder is available through Carthage.

github "SDWebImage/SDWebImageSVGNativeCoder" 

Swift Package Manager

SDWebImageSVGNativeCoder is available through Swift Package Manager.

letpackage=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 letSVGCoder=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:niloptions:0context:@{SDWebImageContextThumbnailPixelSize: @(bitmapSize)];
  • Swift
letimageView:UIImageViewletbitmapSize=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.