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

Feature/uiimage support #382

Closed
wants to merge 2 commits into from
Closed

Feature/uiimage support #382

wants to merge 2 commits into from

Conversation

evnaz
Copy link

@evnaz evnaz commented May 30, 2018

Hi,

I'm trying to implement an extension to create UIImage from SVG file.
I used NodeRenderer.renderToImage() for that. For some reason the image is always empty.
Can anybody advise me what is wrong here? Are there any plans to have such functionality?

Thanks

@ystrot
Copy link
Member

ystrot commented May 31, 2018

Hi Eugene,

You don't need to write your extension, you can use something like this:

func svgToImge(resourceName: String, size: CGSize) -> UIImage {
    let rootNode = SVGParser.parse(path: resourceName)
    let macawView = MacawView(node: rootNode, frame:CGRect(origin: CGPoint.zero, size: size))
    UIGraphicsBeginImageContext(size)
    macawView.layer.render(in: UIGraphicsGetCurrentContext()!)
    let img =  UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img!
}

@evnaz
Copy link
Author

evnaz commented Jun 5, 2018

Hi @ystrot,

The benefits of an UIImage extension are

  • avoiding redundant image drawing (since the SVG's already drawn inside the MacawView)
  • having easy to use interface for image creating

Wouldn't it be great to have such feature as a part of the lib and not a workaround?

@Quanhua-Guan
Copy link

when use MacawView, it has to be in main thread. however, we need to get the image in background thread. @ystrot

@BrettAtMangoHealth
Copy link

Hi @ystrot,
Similarly to @Quanhua-Guan, our team is trying to retrieve the image in a background thread and cannot use a MacawView + the code snippet you provided to get the job done as it results in a blank (and black) image.

@lubiluk
Copy link

lubiluk commented Oct 11, 2018

I've noticed that turning an SVG into UIImage works only when MacawView used for that conversion is actually added to view hierarchy. I guess that doesn't satisfy many use cases.

@amarunko
Copy link
Contributor

Hello, @lubiluk, why do you think so? For me this extension working for a case, when MacawView not in view hierarchy right now. Can you explain your problem if it exists in little details?

@lubiluk
Copy link

lubiluk commented Oct 11, 2018

Now when I tested it again it renders an image just fine. Sorry!

@ystrot ystrot self-assigned this Nov 1, 2018
@ystrot
Copy link
Member

ystrot commented Nov 1, 2018

Hi Eugene,

Thanks you for your proposal. Now this feature available through Node.toNativeImage(size:layout:) method.

Sorry we didn't take your original PR. Final implementation was quite different from the solution you provided, in particular:

  • Node extension is more natural for Macaw API than UIImage extension.
  • It's better to convert node rather than SVG file to an image, because you can create a scene without using SVG.
  • Such a method should also handle content layout properly, etc.

Anyway, let us know if you'll find something else to improve in Macaw and we'll be happy to approve your work next time.

@ystrot ystrot closed this Nov 1, 2018
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.

None yet

6 participants