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

Retrieving an NSImage from an SVGView - no sample code for Cocoa #487

Closed
jcnolan opened this issue Oct 9, 2018 · 5 comments
Closed

Retrieving an NSImage from an SVGView - no sample code for Cocoa #487

jcnolan opened this issue Oct 9, 2018 · 5 comments

Comments

@jcnolan
Copy link

jcnolan commented Oct 9, 2018

I'm working w/Macaw and making very good progress but am trying to sort out one last detail for my app which is to render an SVG into an NSImage for display ( specifically in the icon portion of a "Image & Text Table Cell View" in an NSTable View - we're using custom SVGs as standard icons in our tableview)

I found the iOS version of the sample here #382 (comment) but the required UIGraphicsContext functions are not available in Cocoa. I also noticed the MGraphicsContext functions down in MacawView but have had no success accessing them.

Any hints for getting an NSImage out of standard functions in the Macaw library? Also, I'm unable to find any support forum / support group for Macaw - hope it's ok to post questions here.

@amarunko
Copy link
Contributor

Hi, @jcnolan, you can try following code:

import Foundation
import AppKit

func svgToImage(resourceName: String, size: CGSize) -> NSImage? {
    let rootNode = SVGParser.parse(path: resourceName)
    let macawView = MacawView(node: rootNode, frame: CGRect(origin: CGPoint.zero, size: size))
    macawView.wantsLayer = true
    let image = NSImage(size: macawView.bounds.size)
    image.lockFocus()
    if let ctx = NSGraphicsContext.current?.graphicsPort {
        macawView.layer?.render(in: ctx)
    }
    image.unlockFocus()
    return image
}

@jcnolan
Copy link
Author

jcnolan commented Oct 10, 2018

Brilliant, thanks for that... almost perfect, a couple tweaks and it appears to be working for Swift4/Cocoa (on the main thread at least)

    func svgToNSImage(resourceName: String, size: CGSize) -> NSImage? {
        
       if let rootNode = try? SVGParser.parse(path: resourceName) {

            let macawView = MacawView(node: rootNode, frame: CGRect(origin: CGPoint.zero, size: size))
            macawView.wantsLayer = true
        
            let image = NSImage(size: macawView.bounds.size)
            image.lockFocus()
        
            //        if let ctx = NSGraphicsContext.current?.graphicsPort {
            if let ctx = NSGraphicsContext.current?.cgContext {
                // image is drawing upside down, invert it and render
                ctx.translateBy(x: 0, y: size.height)
                ctx.scaleBy(x: 1.0, y: -1.0)
                macawView.layer?.render(in: ctx)
            }
            image.unlockFocus()
            return image
        } else {
           return nil
        }
    }

@jcnolan jcnolan closed this as completed Oct 11, 2018
@jcnolan jcnolan reopened this Jan 20, 2021
@jcnolan
Copy link
Author

jcnolan commented Jan 20, 2021

Greetings all,

I'm back peeking at that project I inquired about a couple years back and it seems that this render function is no longer working. I always get an empty NSImage (just white, I believe). Can you see anything obvious in the solution we worked out that needs to be changed with your current version?

Thanks,
JC

@ystrot
Copy link
Member

ystrot commented Jan 20, 2021

Hi @jcnolan,

Now there is Node.toNativeImage function which should work properly.

@jcnolan
Copy link
Author

jcnolan commented Jan 20, 2021

Perfect... that's got it fixed, thanks!

@jcnolan jcnolan closed this as completed Jan 20, 2021
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