last day (16 days later) » 

1:27 AM
0
Q: How do I get a Path from an Image( )?

KeithI can't find a way to do this but this code shows the concept of what I'm trying to do. I need to draw the system image into a shape. struct MyNoOp: Shape { var drawInfo: DrawInfo var iconName: String init(_ drawInfo: DrawInfo,_ iconName: String) { self.drawInfo = drawInfo ...

 
@vadian this is NOT related since what you pointed out provides a view... not a PATH.
I'll take a look at the new link please note though that the original link does not compile and is overly complicated. I tried to import UIKit but got this. No such module 'UIKit'. My project is a SWiftUI project.
@vadian Also very complicated but doesn't give you the completed path. Provides an outer outline and misses the internal pieces. These system icons are simple not graphics like the pickachu. It works for the Apple logo since everything is outside. Just looking for something simple that will compile in my SwiftUI project.
 
Rob
As vadian suggested, you could use the contour detection feature of the Vision framework. That’s a good general-purpose edge-detection routine (and you can fetch a CGPath from normalizedPath), and probably could be integrated in SwiftUI relatively easily. That having been said, you’re talking about system symbols (which are, fundamentally, vector assets), you can use the SF Symbols app and extract the SVG data from that (but that’s not a runtime solution, but a development-time exercise). That gets you precise vector data. I do not know any way to retrieve that programmatically at runtime.
Maybe you can describe the broader problem you are trying to solve and we might be able to suggest alternative approaches. E.g., if this is intended just to render a system image at a different size, there are other ways to do that. Bottom line, why do you need a path rather than just rendering the image at the appropriate size?
 
@Rob Hey Rob TY. I'm not looking for edge detection since that would miss the center of the icon. Take a look at plumbing for instance. The bottom of the tank and other details would be missed. I have a series of drawing objects that extend a shape and returns a path to draw (func path(in rect: CGRect) -> Path). In this case I want to display the given SF Symbol at the user given size. For now... I'm using this draw objects that I have not yet implemented... a couple of dozen types. In the long run I have future purposes for it for external 2d and 3d drawing types.
 
Rob
“I'm not looking for edge detection since that would miss the center of the icon ” … Hmm. It works fine for me. I don’t see a “plumbing” system image, but I tried image detection with several system images with shapes in shapes (e.g., trash.circle) and edge detection works fine. (The only trick for me is that it doesn’t recognize transparent background, so I had to fill the background and then draw the system image in another color.) IMHO, the problem with edge detection is not shapes-in-shapes, but rather all the noise/jitter introduced when round tripping a vector to bitmap and back.
 
Ugg... so sorry that was my internal name the icon is toilet. Can you show me an example then what the output was and how you called it? On the trash can did you get all the vertical lines? 2 outer and 3 inner lines.
 
Rob
1:31 AM
So, that’s toilet, rendered as a B&W image followed by the results of the path generated by contour detection.
And that’s trash.circle.
 
Hey that looks cool like I want to do.
Do you have the example code?
 
Rob
func foo() {
    let symbolName = "trash.circle"

    // Create a UIImage instance from the system symbol name
    let configuration = UIImage.SymbolConfiguration(font: .systemFont(ofSize: 512))
    guard let image = UIImage(systemName: symbolName, withConfiguration: configuration) else {
        print("image not found")
        return
    }

    let highContrastImage = UIGraphicsImageRenderer(size: image.size).image { _ in
        UIColor.white.setFill()
        UIBezierPath(rect: CGRect(origin: .zero, size: image.size)).fill()
 
ummm not sure about that... need to see if I can get it to return a path
What's your includes?
struct MyNoOp: Shape {
var drawInfo: DrawInfo
var iconName: String

init(_ drawInfo: DrawInfo,_ iconName: String) {
self.drawInfo = drawInfo
self.iconName = iconName
}

// TODO get this to insert the given icon
func path(in rect: CGRect) -> Path {
var path = MyLine( drawInfo ).path( in: rect )
// path.addPath(Image( systemName: iconName).path(in: rect))






return path
}
 
Rob
normalizedPath is the CGPath
 
Cannot find 'UIImage' in scope
 
Rob
1:39 AM
I need to run, but will check in a few hours.
 
My application is a SwiftUI application.
 
Rob
Yeah, the SwiftUI-ization shouldn’t be too hard.
What is the target platform? iOS? macOS?
 
I'm new to SwiftUI... I started some 4 or 5 weeks ago. Was a low level C++ and python programmer before this.... On Mac OS.
I have this drawing program working which is cool. Layers, tools (~8 so far), Drawing area, rulers, ruler grid. Quite a bit of functionality so far.
 
 
4 hours later…
5:12 AM
Just checking are you still around?
 
 
2 hours later…
7:06 AM
I had Chat GPT convert your code to this. but I get an error No exact matches in call to initializer for symbol Path. Chat GPT is off like 80% time for anything complex so not sure about this:

func createSymbolPath(systemName: String, scale: CGFloat) -> Path {
// Get the system symbol as a SwiftUI Image
let symbol = Image(systemName: systemName)

// Render the symbol within a container view
let symbolView = symbol
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 100, height: 100) // Adjust size as needed
 
 
1 hour later…
Rob
8:32 AM
@Keith – I posted an answer with a SwiftUI implementation.
 
 
9 hours later…
5:21 PM
TY for your response. This doesn't compile Cannot find 'ContourError' in scope. I have both of your includes and for some reason the internet doesn't help. I absolutely need to return a path. I gave you the MyNoOp function which as a path function that returns Path. All of my other shapes follow this Shape protocol.
 
 
2 hours later…
Rob
7:35 PM
@Keith – ContourError is an Error enum that I defined in my code snippet in my answer. I suspect that when you were cutting and pasting, you must have neglected to include the ContourError.
You say “I absolutely need a path”. No, what you need from the Vision framework is a CGPath. And from that, you can create Path as shown in my ContentView demo.
FWIW, I've refined my answer a little to abstract platform-specific stuff in the UIImage/NSImage extensions, and thus eliminating duplication of the contour detection code.
 
Rob
7:48 PM
The virtue of abstracting the contour detection stuff into a CGPath extension is that it can be used in not only your SwiftUI example, but also UIKit/AppKit code, too.
 

  last day (16 days later) »