Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #12 - UIGraphicsImageRenderer should applying scaling #13

Merged
merged 1 commit into from
Aug 31, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion SwiftDraw/UIImage+Image.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ public extension SVG {

func rasterize(with size: CGSize? = nil, scale: CGFloat = 0, insets: UIEdgeInsets = .zero) -> UIImage {
let insets = Insets(top: insets.top, left: insets.left, bottom: insets.bottom, right: insets.right)
let (bounds, pixelsWide, pixelsHigh) = makeBounds(size: size, scale: scale, insets: insets)
let (bounds, pixelsWide, pixelsHigh) = makeBounds(size: size, scale: 1, insets: insets)
let f = makeFormat()
f.scale = scale
f.opaque = false
let r = UIGraphicsImageRenderer(size: CGSize(width: pixelsWide, height: pixelsHigh), format: f)
return r.image{
Expand Down
59 changes: 50 additions & 9 deletions SwiftDrawTests/UIImage+ImageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,56 @@ import UIKit

final class UIImageTests: XCTestCase {

func testImageLoads() {
let image = UIImage(svgNamed: "lines.svg", in: .test)
XCTAssertNotNil(image)
}

func testMissingImageDoesNotLoad() {
let image = UIImage(svgNamed: "missing.svg", in: .test)
XCTAssertNil(image)
}
func testImageLoads() {
let image = UIImage(svgNamed: "lines.svg", in: .test)
XCTAssertNotNil(image)
}

func testMissingImageDoesNotLoad() {
let image = UIImage(svgNamed: "missing.svg", in: .test)
XCTAssertNil(image)
}

func testImageSize() throws {
let image = try SVG.parse(#"""
<?xml version="1.0" encoding="UTF-8"?>
<svg width="64" height="64" version="1.1" xmlns="http://www.w3.org/2000/svg">
</svg>
"""#
)

XCTAssertEqual(
image.rasterize(scale: 1).size,
CGSize(width: 64, height: 64)
)
XCTAssertEqual(
image.rasterize(scale: 1).scale,
1
)
XCTAssertEqual(
image.rasterize(scale: 2).size,
CGSize(width: 64, height: 64)
)
XCTAssertEqual(
image.rasterize(scale: 2).scale,
2
)
}
}

#endif

private extension SVG {

static func parse(_ code: String) throws -> SVG {
guard let data = code.data(using: .utf8),
let svg = SVG(data: data) else {
throw InvalidSVG()
}
return svg
}

private struct InvalidSVG: LocalizedError {
var errorDescription: String? = "Invalid SVG"
}
}