Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "SDWebImage/SDWebImage" ~> 5.0
github "SDWebImage/SDWebImage" ~> 5.5
4 changes: 3 additions & 1 deletion Example/SDWebImageSVGCoder/SDViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ - (void)viewDidLoad
UIImageView *imageView1 = [[UIImageView alloc] init];
imageView1.frame = CGRectMake(0, 0, screenSize.width, screenSize.height / 2);
imageView1.contentMode = UIViewContentModeScaleAspectFit;
imageView1.clipsToBounds = YES;

UIImageView *imageView2 = [[UIImageView alloc] init];
imageView2.frame = CGRectMake(0, screenSize.height / 2, screenSize.width, screenSize.height / 2);
imageView2.contentMode = UIViewContentModeScaleAspectFit;
imageView2.clipsToBounds = YES;

UIImageView *imageView3 = [[UIImageView alloc] init];
imageView3.frame = CGRectMake(screenSize.width - 100, screenSize.height - 100, 100, 100);
Expand Down Expand Up @@ -63,7 +65,7 @@ - (void)viewDidLoad
}];
}
}];
[imageView3 sd_setImageWithURL:svgURL3 placeholderImage:nil options:SDWebImageRetryFailed context:@{SDWebImageContextSVGPrefersBitmap: @(YES), SDWebImageContextSVGImageSize: @(CGSizeMake(100, 100))} progress:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
[imageView3 sd_setImageWithURL:svgURL3 placeholderImage:nil options:SDWebImageRetryFailed context:@{SDWebImageContextImageThumbnailPixelSize: @(CGSizeMake(100, 100))} progress:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
if (image) {
NSLog(@"SVG bitmap load success.");
NSData *svgData = [image sd_imageDataAsFormat:SDImageFormatSVG];
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let package = Package(
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/SDWebImage/SDWebImage.git", from: "5.1.0")
.package(url: "https://github.com/SDWebImage/SDWebImage.git", from: "5.5.0")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand Down
28 changes: 17 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ SDWebImageSVGCoder is available through [Swift Package Manager](https://swift.or
```swift
let package = Package(
dependencies: [
.package(url: "https://github.com/SDWebImage/SDWebImageSVGCoder.git", from: "1.0")
.package(url: "https://github.com/SDWebImage/SDWebImageSVGCoder.git", from: "1.4")
]
)
```
Expand Down Expand Up @@ -108,24 +108,26 @@ Note since UIImageView/NSImageView support this vector rendering, it means this

### Render SVG as bitmap image

In most cases, vector SVG is preferred. But however, sometimes you may want the bitmap form of SVG, used for image processing. You can use `.svgPrefersBitmap` to provide the bitmap format as well.
In most cases, vector SVG is preferred. But however, sometimes you may want the bitmap form of SVG, used for image processing.

By default it use the SVG canvas size. You can also 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.
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.

Note: Once you pass the pixel size, we will always generate the bitmap representation even on iOS/tvOS 11+. If you want the vector format, do not pass them, let `UIImageView` to dynamically stretch the SVG.

+ Objective-C

```objectivec
UIImageView *imageView;
CGSize SVGImageSize = CGSizeMake(500, 500);
[imageView sd_setImageWithURL:url placeholderImage:nil options:0 context:@{SDWebImageContextSVGPrefersBitmap: @YES, SDWebImageContextSVGImageSize: @(SVGImageSize)];
CGSize bitmapSize = CGSizeMake(500, 500);
[imageView sd_setImageWithURL:url placeholderImage:nil options:0 context:@{SDWebImageContextThumbnailPixelSize: @(bitmapSize)];
```

+ Swift

```swift
let imageView: UIImageView
let SVGImageSize = CGSize(width: 500, height: 500)
imageView.sd_setImage(with: url, placeholderImage: nil, options: [], context: [.svgPrefersBitmap : true, .svgImageSize : SVGImageSize])
let bitmapSize = CGSize(width: 500, height: 500)
imageView.sd_setImage(with: url, placeholderImage: nil, options: [], context: [.imageThumbnailPixelSize : bitmapSize])
```

## Export SVG data
Expand All @@ -137,15 +139,19 @@ Note: The bitmap form of SVG does not support SVG data export.
+ Objective-C

```objectivec
UIImage *image = imageView.image; // Image generated by this coder plugin
NSData *imageData = [image sd_imageDataAsFormat:SDImageFormatSVG];
UIImage *svgImage; // UIImage with vector image, or NSImage contains `NSSVGImageRep`
if (svgImage.sd_isVector) { // This API available in SDWebImage 5.6.0
NSData *svgData = [svgImage sd_imageDataAsFormat:SDImageFormatSVG];
}
```

+ Swift

```swift
let image = imageView.image // Image generated by this coder plugin
let imageData = image.sd_imageData(as: .SVG)
let svgImage: UIImage // UIImage with vector image, or NSImage contains `NSSVGImageRep`
if svgImage.sd_isVector { // This API available in SDWebImage 5.6.0
let svgData = svgImage.sd_imageData(as: .SVG)
}
```

## Backward Deployment
Expand Down
2 changes: 1 addition & 1 deletion SDWebImageSVGCoder.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ TODO: Add long description of the pod here.
'DERIVE_MACCATALYST_PRODUCT_BUNDLE_IDENTIFIER' => 'NO'
}

s.dependency 'SDWebImage/Core', '~> 5.0'
s.dependency 'SDWebImage/Core', '~> 5.5'
end
20 changes: 17 additions & 3 deletions SDWebImageSVGCoder/Classes/SDImageSVGCoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,36 @@ - (UIImage *)decodedImageWithData:(NSData *)data options:(SDImageCoderOptions *)
BOOL prefersBitmap = NO;
CGSize imageSize = CGSizeZero;
BOOL preserveAspectRatio = YES;

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
// Parse args
SDWebImageContext *context = options[SDImageCoderWebImageContext];
if (context[SDWebImageContextSVGPrefersBitmap]) {
prefersBitmap = [context[SDWebImageContextSVGPrefersBitmap] boolValue];
}
if (context[SDWebImageContextSVGImageSize]) {
prefersBitmap = YES;
NSValue *sizeValue = context[SDWebImageContextSVGImageSize];
#if SD_MAC
imageSize = sizeValue.sizeValue;
#else
imageSize = sizeValue.CGSizeValue;
#endif
} else if (options[SDImageCoderDecodeThumbnailPixelSize]) {
prefersBitmap = YES;
NSValue *sizeValue = options[SDImageCoderDecodeThumbnailPixelSize];
#if SD_MAC
imageSize = sizeValue.sizeValue;
#else
imageSize = sizeValue.CGSizeValue;
#endif
} else if (context[SDWebImageContextSVGPrefersBitmap]) {
prefersBitmap = [context[SDWebImageContextSVGPrefersBitmap] boolValue];
}
if (context[SDWebImageContextSVGImagePreserveAspectRatio]) {
preserveAspectRatio = [context[SDWebImageContextSVGImagePreserveAspectRatio] boolValue];
} else if (options[SDImageCoderDecodePreserveAspectRatio]) {
preserveAspectRatio = [context[SDImageCoderDecodePreserveAspectRatio] boolValue];
}
#pragma clang diagnostic pop

UIImage *image;
if (!prefersBitmap && [self.class supportsVectorSVGImage]) {
Expand Down
9 changes: 5 additions & 4 deletions SDWebImageSVGCoder/Classes/SDWebImageSVGCoderDefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@
/**
A BOOL value which specify whether we prefer the actual bitmap representation instead of vector representation for SVG image. This is because the UIImage on iOS 13+ (NSImage on macOS 10.15+) can use the vector image format, which support dynamic scale without losing any detail. However, for some image processing logic, user may need the actual bitmap representation to manage pixels. (NSNumber)
If you don't provide this value, use NO for default value and prefer the vector format when possible.
@note Deprecated, use `SDWebImageContextImageThumbnailPixelSize`. Pass CGSize.zero means the viewBox size of SVG.
*/
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextSVGPrefersBitmap API_AVAILABLE(macos(10.15), ios(13.0), watchos(6.0), tvos(13.0));
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextSVGPrefersBitmap API_DEPRECATED_WITH_REPLACEMENT("SDWebImageContextImageThumbnailPixelSize", macos(10.15, 10.15), ios(13.0, 13.0), watchos(6.0, 6.0), tvos(13.0, 13.0));

/**
A CGSize raw value which specify the desired SVG image size during image loading. Because vector image like SVG format, may not contains a fixed size, or you want to get a larger size bitmap representation UIImage. (NSValue)
If you don't provide this value, use viewBox size of SVG for default value;
If you don't provide this value, use viewBox size of SVG for default value.
*/
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextSVGImageSize API_AVAILABLE(macos(10.15), ios(13.0), watchos(6.0), tvos(13.0));
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextSVGImageSize API_DEPRECATED_WITH_REPLACEMENT("SDWebImageContextImageThumbnailPixelSize", macos(10.15, 10.15), ios(13.0, 13.0), watchos(6.0, 6.0), tvos(13.0, 13.0));

/**
A BOOL value which specify the whether SVG image should keep aspect ratio during image loading. Because when you specify image size via `SDWebImageContextSVGImageSize`, we need to know whether to keep aspect ratio or not when image size aspect ratio is not equal to SVG viewBox size aspect ratio. (NSNumber)
If you don't provide this value, use YES for default value.
*/
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextSVGImagePreserveAspectRatio API_AVAILABLE(macos(10.15), ios(13.0), watchos(6.0), tvos(13.0));
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextSVGImagePreserveAspectRatio API_DEPRECATED_WITH_REPLACEMENT("SDWebImageContextImagePreserveAspectRatio", macos(10.15, 10.15), ios(13.0, 13.0), watchos(6.0, 6.0), tvos(13.0, 13.0));