Search code examples
macoscocoansimage

How to draw a colored border around NSImage?


I did some research but all the answers I got were for iOS, how can I draw a colored border around NSImage in OSX app? I tried using imageView property of NSImage to set it's border width and color, but it doesn't seem to be working...

Any kind of help is highly appreciated!


Solution

  • Try like this without creating subclass also it is possible. Also you can set the width ans radius accordingly:-

    [imgView setWantsLayer:YES];
    imgView.layer.borderWidth = 1.0;
    imgView.layer.cornerRadius = 8.0;
    imgView.layer.masksToBounds = YES;
    CGColorRef color = CGColorRetain([NSColor colorWithCalibratedRed:0 green:100 blue:0 alpha:0.5f].CGColor);
    [imgView.layer setBorderColor:color];