Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering the same image with different sizes

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.

like image 869
Madhur Ahuja Avatar asked Jan 14 '16 03:01

Madhur Ahuja


1 Answers

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.

like image 110
Jim Correia Avatar answered Oct 04 '22 22:10

Jim Correia