Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 如何在NSImage周围绘制彩色边框?_Macos_Cocoa_Nsimage - Fatal编程技术网

Macos 如何在NSImage周围绘制彩色边框?

Macos 如何在NSImage周围绘制彩色边框?,macos,cocoa,nsimage,Macos,Cocoa,Nsimage,我做了一些研究,但我得到的所有答案都是针对iOS的,我如何在OSX应用程序中在NSImage周围绘制彩色边框?我尝试使用NSImage的imageView属性设置它的边框宽度和颜色,但它似乎不起作用 非常感谢您的任何帮助 您可以将NSView子类化并执行如下操作: - (void)drawRect:(NSRect)dirtyRect { [[NSColor orangeColor] set]; NSBezierPath *path = [NSBezierPath bezierP

我做了一些研究,但我得到的所有答案都是针对iOS的,我如何在OSX应用程序中在NSImage周围绘制彩色边框?我尝试使用NSImage的imageView属性设置它的边框宽度和颜色,但它似乎不起作用


非常感谢您的任何帮助

您可以将NSView子类化并执行如下操作:

- (void)drawRect:(NSRect)dirtyRect
{
    [[NSColor orangeColor] set];

    NSBezierPath *path = [NSBezierPath bezierPath];
    [path appendBezierPathWithRoundedRect:outerFrame xRadius:5 yRadius:5];
    [path setLineWidth:4.0];
    [path stroke];
}

当然,根据是否需要圆角来更改半径值。

尝试这样做而不创建子类也是可能的。还可以相应地设置宽度和半径:-

[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];

在Swift 3中:

imagePreview.layer!.borderWidth = 10.0
imagePreview.layer!.cornerRadius = 8.0
imagePreview.layer!.masksToBounds = true
let color: CGColor = NSColor.blue.cgColor
imagePreview.layer!.borderColor = color
override func draw(_ dirtyRect: NSRect) {
    // Draw Border
    borderColor.set()
    let borderPath = NSBezierPath(rect: dirtyRect)
    borderPath.lineWidth = 2.0
    borderPath.stroke()
}

在swift中:

imagePreview.layer!.borderWidth = 10.0
imagePreview.layer!.cornerRadius = 8.0
imagePreview.layer!.masksToBounds = true
let color: CGColor = NSColor.blue.cgColor
imagePreview.layer!.borderColor = color
override func draw(_ dirtyRect: NSRect) {
    // Draw Border
    borderColor.set()
    let borderPath = NSBezierPath(rect: dirtyRect)
    borderPath.lineWidth = 2.0
    borderPath.stroke()
}

如何设置NSImageView Hussain先生的背景图像。在上面的代码中,我试图用[NSColor colorWithPatternImage:…]替换colorWithPatternImage:…]校准的颜色,但这似乎不起作用,请帮助我,谢谢:)为什么要使用colorWithPatternImage???@HussainShabbir我想为NSImageView设置背景图像,我的意思是我有一个字母图像,应该放在一个框(图像)中。我可以在ios中实现它,但在mac os中很难实现,这是我的实现代码。请帮助我,谢谢:)@HussainShabbir我解决了我的问题,谢谢关心..返回图像后解锁焦点解决了问题:)