-
Notifications
You must be signed in to change notification settings - Fork 551
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
Comments
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
} |
Brilliant, thanks for that... almost perfect, a couple tweaks and it appears to be working for Swift4/Cocoa (on the main thread at least)
|
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, |
Hi @jcnolan, Now there is |
Perfect... that's got it fixed, thanks! |
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.
The text was updated successfully, but these errors were encountered: