Skip to content

Commit 7f8b3cf

Browse files
committed
Issue #12 - UIGraphicsImageRenderer should applying scaling
1 parent 599299a commit 7f8b3cf

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

SwiftDraw/UIImage+Image.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ public extension SVG {
8585

8686
func rasterize(with size: CGSize? = nil, scale: CGFloat = 0, insets: UIEdgeInsets = .zero) -> UIImage {
8787
let insets = Insets(top: insets.top, left: insets.left, bottom: insets.bottom, right: insets.right)
88-
let (bounds, pixelsWide, pixelsHigh) = makeBounds(size: size, scale: scale, insets: insets)
88+
let (bounds, pixelsWide, pixelsHigh) = makeBounds(size: size, scale: 1, insets: insets)
8989
let f = makeFormat()
90+
f.scale = scale
9091
f.opaque = false
9192
let r = UIGraphicsImageRenderer(size: CGSize(width: pixelsWide, height: pixelsHigh), format: f)
9293
return r.image{

SwiftDrawTests/UIImage+ImageTests.swift

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,56 @@ import UIKit
3737

3838
final class UIImageTests: XCTestCase {
3939

40-
func testImageLoads() {
41-
let image = UIImage(svgNamed: "lines.svg", in: .test)
42-
XCTAssertNotNil(image)
43-
}
44-
45-
func testMissingImageDoesNotLoad() {
46-
let image = UIImage(svgNamed: "missing.svg", in: .test)
47-
XCTAssertNil(image)
48-
}
40+
func testImageLoads() {
41+
let image = UIImage(svgNamed: "lines.svg", in: .test)
42+
XCTAssertNotNil(image)
43+
}
44+
45+
func testMissingImageDoesNotLoad() {
46+
let image = UIImage(svgNamed: "missing.svg", in: .test)
47+
XCTAssertNil(image)
48+
}
49+
50+
func testImageSize() throws {
51+
let image = try SVG.parse(#"""
52+
<?xml version="1.0" encoding="UTF-8"?>
53+
<svg width="64" height="64" version="1.1" xmlns="http://www.w3.org/2000/svg">
54+
</svg>
55+
"""#
56+
)
57+
58+
XCTAssertEqual(
59+
image.rasterize(scale: 1).size,
60+
CGSize(width: 64, height: 64)
61+
)
62+
XCTAssertEqual(
63+
image.rasterize(scale: 1).scale,
64+
1
65+
)
66+
XCTAssertEqual(
67+
image.rasterize(scale: 2).size,
68+
CGSize(width: 64, height: 64)
69+
)
70+
XCTAssertEqual(
71+
image.rasterize(scale: 2).scale,
72+
2
73+
)
74+
}
4975
}
5076

5177
#endif
78+
79+
private extension SVG {
80+
81+
static func parse(_ code: String) throws -> SVG {
82+
guard let data = code.data(using: .utf8),
83+
let svg = SVG(data: data) else {
84+
throw InvalidSVG()
85+
}
86+
return svg
87+
}
88+
89+
private struct InvalidSVG: LocalizedError {
90+
var errorDescription: String? = "Invalid SVG"
91+
}
92+
}

0 commit comments

Comments
 (0)