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

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

Open
aram-azbekian opened this issue Oct 18, 2021 · 5 comments

Comments

@aram-azbekian
Copy link

aram-azbekian commented Oct 18, 2021

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
Copy link

@aram-azbekian any chance you found a solution?

@orelsgitconstru
Copy link

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

@gunhansancar
Copy link

@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
Copy link

@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
Copy link

@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.

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

No branches or pull requests

3 participants