From 5e53d59c85ac196af1806f4b116b801b863bb2d1 Mon Sep 17 00:00:00 2001 From: onevcat Date: Sun, 15 Dec 2024 23:32:37 +0900 Subject: [PATCH 1/3] Fallback to size of image if no pixel size can be determined --- Sources/Image/Image.swift | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Sources/Image/Image.swift b/Sources/Image/Image.swift index d1e9350d1..d8da69742 100644 --- a/Sources/Image/Image.swift +++ b/Sources/Image/Image.swift @@ -93,11 +93,18 @@ extension KingfisherWrapper where Base: KFCrossPlatformImage { } var size: CGSize { - return base.representations.reduce(.zero) { size, rep in - let width = max(size.width, CGFloat(rep.pixelsWide)) - let height = max(size.height, CGFloat(rep.pixelsHigh)) - return CGSize(width: width, height: height) + let pixelSize = base.representations.reduce(.zero) { size, rep in + CGSize( + width: max(size.width, CGFloat(rep.pixelsWide)), + height: max(size.height, CGFloat(rep.pixelsHigh)) + ) } + return pixelSize == .zero ? base.representations.reduce(.zero) { size, rep in + CGSize( + width: max(size.width, CGFloat(rep.size.width)), + height: max(size.height, CGFloat(rep.size.height)) + ) + } : pixelSize } #else var cgImage: CGImage? { return base.cgImage } From 00a5645bd2c3b2f4f7dae8a8d7764d161e71157a Mon Sep 17 00:00:00 2001 From: onevcat Date: Sun, 15 Dec 2024 23:44:41 +0900 Subject: [PATCH 2/3] Add a test case for svg on macOS --- .../KingfisherTests/ImageExtensionTests.swift | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Tests/KingfisherTests/ImageExtensionTests.swift b/Tests/KingfisherTests/ImageExtensionTests.swift index 64d61ae10..41986e605 100644 --- a/Tests/KingfisherTests/ImageExtensionTests.swift +++ b/Tests/KingfisherTests/ImageExtensionTests.swift @@ -344,4 +344,26 @@ class ImageExtensionTests: XCTestCase { // You can not "downsample" an image to a larger size. XCTAssertEqual(largerImage?.size, CGSize(width: 64, height: 64)) } + + #if os(macOS) + func testSVGImageSize() { + let svgString = """ + + + + + """ + + guard let data = svgString.data(using: .utf8), + let image = NSImage(data: data) + else { + XCTFail("Failed to create image from SVG data") + return + } + + let size = image.kf.size + XCTAssertEqual(size.width, 100) + XCTAssertEqual(size.height, 200) + } + #endif } From 55266a43b58084cfde84030edd9b06db0c566855 Mon Sep 17 00:00:00 2001 From: onevcat Date: Sun, 15 Dec 2024 23:46:53 +0900 Subject: [PATCH 3/3] Add two comments for memo --- Sources/Image/Image.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sources/Image/Image.swift b/Sources/Image/Image.swift index d8da69742..7568ffbf3 100644 --- a/Sources/Image/Image.swift +++ b/Sources/Image/Image.swift @@ -93,12 +93,14 @@ extension KingfisherWrapper where Base: KFCrossPlatformImage { } var size: CGSize { + // Prefer to use pixel size of the image let pixelSize = base.representations.reduce(.zero) { size, rep in CGSize( width: max(size.width, CGFloat(rep.pixelsWide)), height: max(size.height, CGFloat(rep.pixelsHigh)) ) } + // If the pixel size is zero (SVG or PDF, for example), use the size of the image. return pixelSize == .zero ? base.representations.reduce(.zero) { size, rep in CGSize( width: max(size.width, CGFloat(rep.size.width)),