Is there a way to access an Icon Composer .icon file in Swift or Objective-C? Any way to get this in an NSImage object that I can display in an image view? Thanks.
Access .icon files in Swift or Objective-C
You can use
Swift
NSImage(named:"Icon_Composer_Name")
or
Objective-C
[NSImage imageNamed:@"Icon_Composer_Name"]
to access the icon Composer file.
In the example below, an image view displays an image that uses an Icon Composer file named "AppIcon-Blue" if found.
if let image = NSImage(named: "AppIcon-Blue") {
myImageView.image = image
}
Hi @DTS Engineer!
Unfortunately this seems not to work anymore. I remember it worked until beta 3 or so but now the returned NSImage object is just null.
Unfortunately this seems not to work anymore. I remember it worked until beta 3 or so but now the returned NSImage object is just null
Is the app icon set as the App Icon in the App Icons and Launch Screen section of the target building your app?
@DTS Engineer I actually want to use these .icon files in a Dock Tile plugin to change the icon of a Dock Tile depending on certain conditions. I added the files to the target, and the icons seem to find their way into the asset catalog, but I cannot access them.
@DTS Engineer I actually want to use these .icon files in a Dock Tile plugin to change the icon of a Dock Tile depending on certain conditions.
That complicates things. The issues here are:
-
Like an app extension, your Dock Tile plug is a separate component from the main app, which makes it somewhat ambiguous about exactly files it should/should not have access to.
-
Unlike a true app extension, your Dock Tile is actually running as a library loaded by an apple executable, NOT as an independently sized, "standalone" executable.
That second point is the critical one here, as "classic" plugins are increasingly rare, making it easier for us to break things.
I added the files to the target, and the icons seem to find their way into the asset catalog, but I cannot access them.
A few points here:
-
Is the icon catalog inside the dock tile plugin or inside the main app bundle? You should be able to access this if it's inside the dock tile plugin, but outside of the bundle things are... less clear.
-
Does the same code work in your main app? Before you bother debugging the plugin side, double check that the "basic" case works properly.
-
Are you able to load standard image files (like png) from the same location?
-
What are you actually using this plugin "for"? Keep in mind that dock tile plugins generally aren't allowed on the app store (because of #2 above) and you don't need to use a plugin at all while your app is actually running.
__
Kevin Elliott
DTS Engineer, CoreOS/Hardware
Hi @DTS Engineer!
Yes, the asset catalog is inside the plugin bundle and I can access other assets (png) without problems. To make things easier I created a simple app and added some .icon files to it. First I tried to access them using imageNamed:
which doesn't work. Then I copied the icon files to the app's Resources
folder, got the actual image path using [[NSBundle mainBundle] pathForResource:@"MyIcon" ofType:@"icon"]
(which worked) and then to get an image using [[NSImage alloc] initWithContentsOfFile:iconPath]
which returned null. So the issue is not about the Dock Tile plug in but a more general issue.
So the issue is not about the Dock Tile plug-in but a more general issue.
With help from a colleague, I think we figured things out here.
So, the "expected" behavior of the asset catalog is that it only handles the app icon(s), as specified through the Info.plist. However, the "Include all app icon assets" checkbox in the General target setting will merge the resources in, at which point this should start working.
Having said that, I'm not sure you wouldn't be better off using a regular image asset. You're ultimately drawing into a view; the icon asset itself isn't really providing much of a benefit. More to the point, the fact this works at all isn't entirely "intentional", which is never ideal.
__
Kevin Elliott
DTS Engineer, CoreOS/Hardware
Enabling Include all app icon assets
did the trick and I was able to access the icon by its name. Unfortunately, the icon is always rendered in its default appearance and does not adopt the current appearance, such as dark or tinted. Any chance to make this work? Is there a supported way to
- get the current appearance
- request the image in a specific appearance
- receive a notification if the appearance changes?
The last point would also be necessary if I were using a regular asset set with PNG representations of the .icon file.
Thanks, Marc
Any chance to make this work? Is there a supported way to
- get the current appearance
- request the image in a specific appearance
- receive a notification if the appearance changes?
I'm not sure if it will work with icon assets specifically, but "Supporting Dark Mode in your interface" and "Providing images for different appearances" cover all of these issues. Note that while both of those articles are in the UIKit section, they also cover AppKit.
__
Kevin Elliott
DTS Engineer, CoreOS/Hardware
I'm not sure if it will work with icon assets specifically, but "Supporting Dark Mode in your interface" and "Providing images for different appearances" cover all of these issues. Note that while both of those articles are in the UIKit section, they also cover AppKit.
Thanks, but unfortunately this does not work with the new icon styles.
Best regards, Marc