exyte / Macaw

Powerful and easy-to-use vector graphics Swift library with SVG support
MIT License
6.02k stars 560 forks source link

SVGView doesn't fit the image in the screen with contentMode ".scaleAspectFit" #776

Open aram-azbekian opened 3 years ago

aram-azbekian commented 3 years ago

Hi! I have a small project where I'm trying to open SVG image using Macaw library. In that project I make a SVGView and set its contentMode parameter to ".scaleAspectFit". As far as I know, the only condition for it to work is availability of "width" and "height" parameters in SVG XML. But it just doesn't work and the image is presented in its original scale

So, this is a code snippet where I create a view:

func makeUIView(context: Context) -> SVGView {
        let node = try! SVGParser.parse(resource: svgName)
        let svgView = SVGView(node: node, frame: CGRect(origin: CGPoint.zero, size: size))
        svgView.backgroundColor = UIColor.white
        svgView.contentMode = .scaleAspectFit
        svgView.layer.borderWidth = 1.0
        svgView.layer.borderColor = UIColor.white.cgColor
        svgView.zoom.enable()

        return svgView
}

and attached below is the image example from the project

Thanks in advance. Let me know if you need anything else. svg-5.svg.zip

gunhansancar commented 2 years ago

@aram-azbekian any chance you found a solution?

orelsgitconstru commented 2 years ago

@gunhansancar @aram-azbekian Any chance you found a solution? :P

gunhansancar commented 2 years ago

@orelsgitconstru Not really unfortunately. I ended up converting the Node to regular UIImage so that I can use it in anywhere. It is not optimal but at least it works.

node.toNativeImage(size: Size(width, height))

Here also their toNativeImage method sets to scale to 1.0 for some reason so you have to multiple width, height of the node with actual screen.scale to end up with pixel perfect image.

Like the following:

let size = node.bounds?.size() ?? Size.zero
let scale = UIScreen.main.scale
let width = size.w * scale
let height = size.h * scale
let image = try node.toNativeImage(size: Size(width, height))
orelsgitconstru commented 2 years ago

@aram-azbekian any chance you found a solution?

I found a solution. Inside your svg, first row, there's width and height. Change them.

orelsgitconstru commented 2 years ago

@orelsgitconstru Not really unfortunately. I ended up converting the Node to regular UIImage so that I can use it in anywhere. It is not optimal but at least it works.

node.toNativeImage(size: Size(width, height))

Here also their toNativeImage method sets to scale to 1.0 for some reason so you have to multiple width, height of the node with actual screen.scale to end up with pixel perfect image.

Like the following:

let size = node.bounds?.size() ?? Size.zero
let scale = UIScreen.main.scale
let width = size.w * scale
let height = size.h * scale
let image = try node.toNativeImage(size: Size(width, height))

Nice idea, wow.