0
votes

I have a NSCollectionViewItem with NSImageView in it.

I want to have a shadow around the image. Once it is selected, I want the image to have shadow and a border. Currently, NSImageView gets a border around the image.

How can I achieve shadow + border on selection? Seems like the border around image sets up correctly, but the border sets up around the image view.

[self.templateImageView setWantsLayer:YES];
self.templateImageView.layer.borderWidth = 0.0;
self.templateImageView.layer.borderColor = borderColor.CGColor;
self.templateImageView.layer.masksToBounds = YES;

[self.templateImageView.layer setShadowColor: [NSColor blackColor].CGColor];
[self.templateImageView.layer setShadowOpacity:0.8];
[self.templateImageView.layer setShadowRadius:5.0];
2

2 Answers

2
votes

First, your image must have a transparent background. Then you can draw a border around the content of images:

func drawOutlie(image:UIImage, color:UIColor) -> UIImage
{
    let newImageKoef:CGFloat = 1.08

    let outlinedImageRect = CGRect(x: 0.0, y: 0.0, width: image.size.width * newImageKoef, height: image.size.height * newImageKoef)

    let imageRect = CGRect(x: image.size.width * (newImageKoef - 1) * 0.5, y: image.size.height * (newImageKoef - 1) * 0.5, width: image.size.width, height: image.size.height)

    UIGraphicsBeginImageContextWithOptions(outlinedImageRect.size, false, newImageKoef)

    image.draw(in: outlinedImageRect)

    let context = UIGraphicsGetCurrentContext()
    context!.setBlendMode(CGBlendMode.sourceIn)

    context!.setFillColor(color.cgColor)
    context!.fill(outlinedImageRect)
    image.draw(in: imageRect)

    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImage!

}

You can change outline size by changingnewImageKoef.

The answer is based on haawa answer and update to swift 3.0.

0
votes

Thanks for an answer. It did help find what I was looking for. Here is a solution:

- (NSImage*)borderAroundImage:(NSImage*)image
{
NSSize size = NSMakeSize([image size].width, [image size].height);

NSImage* im = image;
NSBitmapImageRep* rep = [[NSBitmapImageRep alloc]
                         initWithBitmapDataPlanes:NULL
                         pixelsWide:size.width
                         pixelsHigh:size.height
                         bitsPerSample:8
                         samplesPerPixel:4
                         hasAlpha:YES
                         isPlanar:NO
                         colorSpaceName:NSCalibratedRGBColorSpace
                         bytesPerRow:0
                         bitsPerPixel:0];

[im addRepresentation:rep];

[im lockFocus];

CGContextRef ctx = [[NSGraphicsContext currentContext] graphicsPort];

CGContextSetStrokeColorWithColor(ctx, [NSColor colorWithDeviceRed:0.200 green:0.529 blue:0.957 alpha:1.000].CGColor);
CGContextSetLineWidth(ctx, 25.0);
CGContextStrokeRect(ctx, CGRectInset(CGRectMake(0, 0, [image size].width, [image size].height), 0, 0));

[im unlockFocus];

return im;
}