Skip to content

Conversation

@akring
Copy link

@akring akring commented Oct 23, 2024

No description provided.

@swhitty
Copy link
Owner

swhitty commented Feb 22, 2025

Hi, thanks for attempting to address this issue. I've double checked and SwiftDraw's code is working correctly — I've added a new unit test to prove it;

When we rasterize an image we provide the size in points (not pixels) and a scale that would typically correlate to the screen scale. The underlying UIImage is created with the right number of pixels, but this is not really visible until you save it to disk and open in an editor.

let svg = SVG(named: "gradient-apple.svg", in: .test)!
let image = svg.rasterize(with: CGSize(width: 100, height: 100), scale: 3)
XCTAssertEqual(image.size, CGSize(width: 100, height: 100))
XCTAssertEqual(image.scale, 3)

You can also load the PNG Data back into a new UIImage and it will show the size in raw pixels.

let data = image.pngData()!
let reloaded = UIImage(data: data)!
XCTAssertEqual(reloaded.size, CGSize(width: 300, height: 300))
XCTAssertEqual(reloaded.scale, 1)

I appreciate this may not be clear from the API and possibly you are looking to instead increase/decrease the size of an SVG? I could offer a "transform" API on SVG to allow this, after applying all transformations you can then rasterize using the existing API. I'm thinking something like this;

let svg = SVG(named: "chart.svg", in: .test)!
let scaled = svg.scale(2.0)

// svg.size == CGSize(width: 160, height: 160)
// scaled.size == CGSize(width: 320, height: 320)

let image = scaled.rasterize()

Copy link
Owner

@swhitty swhitty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI I have made these changes on the main branch allowing the SVG object to be transformed with scale / translate / expand before calling rasterize()

let svg = SVG(name: "fish.svg")!  // 100x100 
    .expand(left: 10, right: 10)  // 120x100
    .scale(2)                     // 240x200

let image = svg.rasterize()       // 240x200

@swhitty swhitty closed this Feb 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants