0

NSImageView가있는 NSCollectionViewItem이 있습니다.NSImageView의 NSCollectionViewItem 선택, CALayer가있는 이미지 주위의 테두리

이미지 주위에 그림자가 나타납니다. 일단 선택되면 이미지에 그림자와 테두리가 있어야합니다. 현재 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]; 

답변

1

먼저 이미지의 배경이 투명해야합니다. 은 그럼 당신은 이미지의 내용 주위에 테두리를 그릴 수 있습니다 :

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 업데이트를 기반으로합니다.

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; 
}