Latest 0.3.0
Homepage https://github.com/SDWebImage/SDWebImageSVGCoder
License MIT
Platforms ios 8.0, tvos 9.0, osx 10.10
Dependencies SDWebImage/Core, SVGKit
Authors

CI Status
Version
License
Platform
Carthage compatible

What’s for

SDWebImageSVGCoder is a SVG coder plugin for SDWebImage framework, which provide the image loading support for SVG using SVGKit SVG engine.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

You can modify the code or use some other SVG files to check the compatibility.

Requirements

  • iOS 8
  • tvOS 9
  • macOS 10.10

Installation

CocoaPods

SDWebImageSVGCoder is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'SDWebImageSVGCoder'

Note: The SVGKit dependency seems does not follow sem-version and didn’t not release versions frequently. You can manually specify a branch or CID dependency for it. Like below:

pod 'SVGKit', :git => 'https://github.com/SVGKit/SVGKit.git', :branch => '3.x'

Carthage

SDWebImageSVGCoder is available through Carthage.

github "SDWebImage/SDWebImageSVGCoder"

Usage

Use UIImageView (render SVG as bitmap image)

To use SVG coder, you should firstly add the SDImageSVGCoder to the coders manager. Then you can call the View Category method to start load SVG images.

Because SVG is a vector image format, which means it does not have a fixed bitmap size. However, UIImage or CGImage are all bitmap image. For UIImageView, we will only parse SVG with a fixed image size (from the SVG viewPort information). But we also support you to specify a desired size during image loading using SVGImageSize context option. And you can specify whether or not to keep aspect ratio during scale using SVGImagePreserveAspectRatio context option.

  • Objective-C
SDImageSVGCoder *SVGCoder = [SDImageSVGCoder sharedCoder];
[[SDImageCodersManager sharedManager] addCoder:SVGCoder];
UIImageView *imageView;
// this arg is optional, if don't provide, use the viewport size instead
CGSize SVGImageSize = CGSizeMake(100, 100);
[imageView sd_setImageWithURL:url placeholderImage:nil options:0 context:@{SDWebImageContextSVGImageSize : @(SVGImageSize)];
  • Swift
let SVGCoder = SDImageSVGCoder.shared
SDImageCodersManager.shared.addCoder(SVGCoder)
let imageView: UIImageView
imageView.sd_setImage(with: url)
// this arg is optional, if don't provide, use the viewport size instead
let SVGImageSize = CGSize(width: 100, height: 100)
imageView.sd_setImage(with: url, placeholderImage: nil, options: [], context: [.svgImageSize : SVGImageSize])

Use SVGKImageView (render SVG as vector image)

SVGKit also provide some built-in image view class for vector image loading (scale to any size without losing detail). The SVGKLayeredImageView && SVGKFastImageView are the subclass of SVGKImageView base class. We supports these image view class as well. You can just use the same API like normal UIImageView.

For the documentation about SVGKLayeredImageView, SVGKFastImageView or SVGKImageView, check SVGKit repo for more information.

Note: If you only use these image view class and don’t use SVG on UIImageView, you don’t need to register the SVG coder to coders manager. These image view loading was using the Custom Image Class feature of SDWebImage.

Attention: These built-in image view class does not works well on UIView.contentMode property, you need to re-scale the layer tree after image was loaded. We provide a simple out-of-box solution to support it. Set the sd_adjustContentMode property to YES then all things done.

  • Objective-C
SVGKImageView *imageView; // can be either `SVGKLayeredImageView` or `SVGKFastImageView`
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.sd_adjustContentMode = YES; // make `contentMode` works
[imageView sd_setImageWithURL:url];
  • Swift:
let imageView: SVGKImageView // can be either `SVGKLayeredImageView` or `SVGKFastImageView`
imageView.contentMode = .aspectFill
imageView.sd_adjustContentMode = true // make `contentMode` works
imageView.sd_setImage(with: url)

Export SVG data

SDWebImageSVGCoder provide an easy way to export the SVG image generated from framework, to the original SVG data.

  • Objective-C
UIImage *image; // Image generated from SDWebImage framework, actually a `SDSVGImage` instance.
NSData *imageData = [image sd_imageDataAsFormat:SDImageFormatSVG];
  • Swift
let image; // Image generated from SDWebImage framework, actually a `SDSVGImage` instance.
let imageData = image.sd_imageData(as: .SVG)

Screenshot

SDWebImageSVGCoder
SDWebImageSVGCoder

These SVG images are from wikimedia, you can try the demo with your own SVG image as well.

Author

DreamPiggy

License

SDWebImageSVGCoder is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "SDWebImageSVGCoder",
    "version": "0.3.0",
    "summary": "A SVG coder plugin for SDWebImage, using SVGKit.",
    "description": "TODO: Add long description of the pod here.",
    "homepage": "https://github.com/SDWebImage/SDWebImageSVGCoder",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "[email protected]": "[email protected]"
    },
    "source": {
        "git": "https://github.com/SDWebImage/SDWebImageSVGCoder.git",
        "tag": "0.3.0"
    },
    "platforms": {
        "ios": "8.0",
        "tvos": "9.0",
        "osx": "10.10"
    },
    "source_files": [
        "SDWebImageSVGCoder/Classes/**/*",
        "SDWebImageSVGCoder/Module/SDWebImageSVGCoder.h"
    ],
    "module_map": "SDWebImageSVGCoder/Module/SDWebImageSVGCoder.modulemap",
    "dependencies": {
        "SDWebImage/Core": [
            "~> 5.0"
        ],
        "SVGKit": [
            ">= 2.1"
        ]
    }
}

Pin It on Pinterest

Share This