9

I have bunch of images in my assets. What I am trying to do is render the image in status bar of OSX as following:

let icon = NSImage(named: "statusIcon")
icon?.size = NSSize.init(width: 18, height: 18)

icon?.template = true
statusItem.image = icon
statusItem.menu = statusMenu

and also using it in one of my view which opens:

self.dayIcon.image = NSImage(named: "statusIcon")

The problem is as soon as I set the status bar image, the image in the view also changes, i.e. both the color and the size(changes to 18x18)

I have tried using

icon?.cacheMode = NSImageCacheMode.Never

but there is no effect.

Is this how it is supposed to be? Can I not use the same image and render it differently at different places. They are both different NSImage instances.

Madhur Ahuja
  • 22,211
  • 14
  • 71
  • 124

2 Answers2

5

They are both different NSImage instance.

They are not, and that is, indeed, your problem.

+[NSImage imageNamed:] may return an existing cached instance of the image.

If you want to change the size on the image without affecting anyone else who may be holding a reference, make a copy of it. The copy of the NSImage is lightweight - it doesn't duplicate the underlying image representations which hold the rendering (bitmap, in the PNG case) data.

Jim Correia
  • 7,064
  • 1
  • 33
  • 24
3

I suppose that you need to resize image first. For doing that you may use different ways. For example you can write extension for NSImage class. For example Resize NSImage in Swift

Community
  • 1
  • 1
toohtik
  • 1,892
  • 11
  • 27
  • Also I sugest you to read nice article about status bar icons sizing: https://alastairs-place.net/blog/2013/07/23/nsstatusitem-what-size-should-your-icon-be/ – toohtik Jan 18 '16 at 16:55