设置模板图像的图像颜色

Gho*_*108 7 macos cocoa nsimageview swift3

我有这样的图像: 在此输入图像描述

(渲染为模板图像)

我试过这段代码:

@IBOutlet weak var imgAdd: NSImageView!
imgAdd.layer?.backgroundColor = CGColor.white
Run Code Online (Sandbox Code Playgroud)

这只会改变背景颜色.

有没有办法以编程方式更改此图像的颜色?


到目前为止,我已经尝试了下面的代码不起作用.(图像颜色不会改变.)

func tintedImage(_ image: NSImage, tint: NSColor) -> NSImage {
    guard let tinted = image.copy() as? NSImage else { return image }
    tinted.lockFocus()
    tint.set()

    let imageRect = NSRect(origin: NSZeroPoint, size: image.size)
    NSRectFillUsingOperation(imageRect, .sourceAtop)

    tinted.unlockFocus()
    return tinted
}

imgDok.image = tintedImage(NSImage(named: "myImage")!, tint: NSColor.red)
Run Code Online (Sandbox Code Playgroud)

小智 13

雨燕5

\n

应该使用此方法:(注意其他答案,NSImage.lockFocus()不推荐使用 macOS 10.0\xe2\x80\x9313.0)

\n
extension NSImage {\n    func tint(color: NSColor) -> NSImage {\n        NSImage(size: size, flipped: false) { rect in\n            color.set()\n            rect.fill()\n            self.draw(in: rect, from: NSRect(origin: .zero, size: self.size), operation: .destinationIn, fraction: 1.0)\n            return true\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

.withAlphaComponent(0.5)注意:在实例上使用时NSColor,该颜色失去对明/暗模式之间切换的支持。建议使用颜色资源,以避免出现此问题。

\n


oco*_*odo 10

斯威夫特4

更新了Swift 4的答案

请注意,此NSImage扩展程序基于@ Ghost108@ Taehyung_Cho的答案,因此可以获得更大的功劳.

extension NSImage {
    func tint(color: NSColor) -> NSImage {
        let image = self.copy() as! NSImage
        image.lockFocus()

        color.set()

        let imageRect = NSRect(origin: NSZeroPoint, size: image.size)
        imageRect.fill(using: .sourceAtop)

        image.unlockFocus()

        return image
    }
}
Run Code Online (Sandbox Code Playgroud)


hkd*_*lex 9

由于您的图像位于 NSImageView 内,因此以下内容应该可以正常工作(自 macOS 10.14 起可用):

let image = NSImage(named: "myImage")!
image.isTemplate = true
let imageView = NSImageView(image: image)
imageView.contentTintColor = .green
Run Code Online (Sandbox Code Playgroud)

解决方案是将“contentTintColor”应用到您的 NSImageView 而不是 NSImage。

请参阅:文档


Tie*_*Van 8

斯威夫特 4 版本

extension NSImage {
   func image(withTintColor tintColor: NSColor) -> NSImage {
       guard isTemplate else { return self }
       guard let copiedImage = self.copy() as? NSImage else { return self }
       copiedImage.lockFocus()
       tintColor.set()
       let imageBounds = NSMakeRect(0, 0, copiedImage.size.width, copiedImage.size.height)
       imageBounds.fill(using: .sourceAtop)
       copiedImage.unlockFocus()
       copiedImage.isTemplate = false
       return copiedImage
   }
}
Run Code Online (Sandbox Code Playgroud)


Gho*_*108 6

在每个人的帮助下我找到了解决方案:

(斯威夫特3)

func tintedImage(_ image: NSImage, tint: NSColor) -> NSImage {
    guard let tinted = image.copy() as? NSImage else { return image }
    tinted.lockFocus()
    tint.set()

    let imageRect = NSRect(origin: NSZeroPoint, size: image.size)
    NSRectFillUsingOperation(imageRect, .sourceAtop)

    tinted.unlockFocus()
    return tinted
}

imgDok.image = tintedImage(NSImage(named: "myImage")!, tint: NSColor.red)
Run Code Online (Sandbox Code Playgroud)

重要提示:在界面构建器中,我必须将图像的"渲染为"设置设置为"默认".


小智 6

不得不为 Xcode 9.2 修改@Ghost108 的答案。

NSRectFillUsingOperation(imageRect, .sourceAtop)
Run Code Online (Sandbox Code Playgroud)

imageRect.fill(using: .sourceAtop)
Run Code Online (Sandbox Code Playgroud)

谢谢。