From 4f0a11ce2783bd6c7066e22c6d584087190d8b33 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Fri, 31 Jan 2020 19:01:21 +0800 Subject: [PATCH 1/2] Update to support the new context option from SDWebImage 5.5.0, deprecated the old one --- Cartfile | 2 +- Example/SDWebImageSVGCoder/SDViewController.m | 2 ++ Package.swift | 2 +- SDWebImageSVGCoder.podspec | 2 +- SDWebImageSVGCoder/Classes/SDImageSVGCoder.m | 20 ++++++++++++++++--- .../Classes/SDWebImageSVGCoderDefine.h | 9 +++++---- 6 files changed, 27 insertions(+), 10 deletions(-) diff --git a/Cartfile b/Cartfile index 9a360e8..cb68a79 100644 --- a/Cartfile +++ b/Cartfile @@ -1 +1 @@ -github "SDWebImage/SDWebImage" ~> 5.0 \ No newline at end of file +github "SDWebImage/SDWebImage" ~> 5.5 \ No newline at end of file diff --git a/Example/SDWebImageSVGCoder/SDViewController.m b/Example/SDWebImageSVGCoder/SDViewController.m index 66c6c7a..ca97766 100644 --- a/Example/SDWebImageSVGCoder/SDViewController.m +++ b/Example/SDWebImageSVGCoder/SDViewController.m @@ -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); diff --git a/Package.swift b/Package.swift index 2347174..7abfc31 100644 --- a/Package.swift +++ b/Package.swift @@ -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. diff --git a/SDWebImageSVGCoder.podspec b/SDWebImageSVGCoder.podspec index b96a1c4..d42d0c3 100644 --- a/SDWebImageSVGCoder.podspec +++ b/SDWebImageSVGCoder.podspec @@ -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 diff --git a/SDWebImageSVGCoder/Classes/SDImageSVGCoder.m b/SDWebImageSVGCoder/Classes/SDImageSVGCoder.m index 274f021..381d05c 100644 --- a/SDWebImageSVGCoder/Classes/SDImageSVGCoder.m +++ b/SDWebImageSVGCoder/Classes/SDImageSVGCoder.m @@ -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]) { diff --git a/SDWebImageSVGCoder/Classes/SDWebImageSVGCoderDefine.h b/SDWebImageSVGCoder/Classes/SDWebImageSVGCoderDefine.h index 6845806..e7a0d41 100644 --- a/SDWebImageSVGCoder/Classes/SDWebImageSVGCoderDefine.h +++ b/SDWebImageSVGCoder/Classes/SDWebImageSVGCoderDefine.h @@ -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)); From 6f14ff2f78c28ce21eadef9d81237370d1c0fbf2 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Fri, 31 Jan 2020 20:06:29 +0800 Subject: [PATCH 2/2] Update the Example and Readme about the new context option --- Example/SDWebImageSVGCoder/SDViewController.m | 2 +- README.md | 28 +++++++++++-------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Example/SDWebImageSVGCoder/SDViewController.m b/Example/SDWebImageSVGCoder/SDViewController.m index ca97766..bfac528 100644 --- a/Example/SDWebImageSVGCoder/SDViewController.m +++ b/Example/SDWebImageSVGCoder/SDViewController.m @@ -65,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]; diff --git a/README.md b/README.md index b177517..881e2a4 100644 --- a/README.md +++ b/README.md @@ -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") ] ) ``` @@ -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 @@ -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