NSImageView 上的 NSCollectionViewItem 选择,带 CALayer 的图像边框
NSCollectionViewItem selection on NSImageView, border around image with CALayer
我有一个带有 NSImageView 的 NSCollectionViewItem。
我想在图像周围添加阴影。选择后,我希望图像具有阴影和边框。目前,NSImageView 获取图像周围的边框。
如何实现选区的阴影+边框?似乎图像周围的边框设置正确,但图像视图周围的边框设置正确。
[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];
首先,您的图像必须具有透明背景。
然后你可以在图像内容周围画一个边框:
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!
}
您可以通过更改newImageKoef
来更改轮廓大小。
答案基于haawa答案并更新至swift 3.0.
感谢您的回答。它确实有助于找到我要找的东西。这是一个解决方案:
- (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;
}
我有一个带有 NSImageView 的 NSCollectionViewItem。
我想在图像周围添加阴影。选择后,我希望图像具有阴影和边框。目前,NSImageView 获取图像周围的边框。
如何实现选区的阴影+边框?似乎图像周围的边框设置正确,但图像视图周围的边框设置正确。
[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];
首先,您的图像必须具有透明背景。 然后你可以在图像内容周围画一个边框:
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!
}
您可以通过更改newImageKoef
来更改轮廓大小。
答案基于haawa答案并更新至swift 3.0.
感谢您的回答。它确实有助于找到我要找的东西。这是一个解决方案:
- (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;
}