为 NSStatusItem 图像缩放 PDF 图像
Scaling a PDF image for NSStatusItem image
我正在尝试创建一个 macOS 应用程序,其状态栏项目使用 PDF 而不是 PNG 图像,但是当我使用该图像时,它被过度缩放并且整个状态项目是黑色的。我找不到一种方法来缩放图像以使其适合 mac 状态栏项目的其余部分。
图标
- Font Awesome
- (我无法附上pdf所以你需要手动转换它)
Assets.xcassets
AppDelegate.swift
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
let statusItem = NSStatusBar.system.statusItem(withLength:NSStatusItem.squareLength)
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
if let button = statusItem.button {
button.image = NSImage(named:NSImage.Name("status-logo"))
button.action = #selector(printQuote(_:))
}
}
@objc func printQuote(_ sender: Any?) {
let quoteText = "Never put off until tomorrow what you can do the day after tomorrow."
let quoteAuthor = "Mark Twain"
print("\(quoteText) — \(quoteAuthor)")
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}
期望值
现实
您应该调整 pdf 的大小,使其适合 statusItem
的按钮,如下所示:
guard let logo = NSImage(named: NSImage.Name("status-logo")) else { return }
let resizedLogo = NSImage(size: NSSize(width: 18, height: 18), flipped: false) { (dstRect) -> Bool in
logo.draw(in: dstRect)
return true
}
然后设置button.image = resizedLogo
我正在尝试创建一个 macOS 应用程序,其状态栏项目使用 PDF 而不是 PNG 图像,但是当我使用该图像时,它被过度缩放并且整个状态项目是黑色的。我找不到一种方法来缩放图像以使其适合 mac 状态栏项目的其余部分。
图标
- Font Awesome
- (我无法附上pdf所以你需要手动转换它)
Assets.xcassets
AppDelegate.swift
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
let statusItem = NSStatusBar.system.statusItem(withLength:NSStatusItem.squareLength)
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
if let button = statusItem.button {
button.image = NSImage(named:NSImage.Name("status-logo"))
button.action = #selector(printQuote(_:))
}
}
@objc func printQuote(_ sender: Any?) {
let quoteText = "Never put off until tomorrow what you can do the day after tomorrow."
let quoteAuthor = "Mark Twain"
print("\(quoteText) — \(quoteAuthor)")
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}
期望值
现实
您应该调整 pdf 的大小,使其适合 statusItem
的按钮,如下所示:
guard let logo = NSImage(named: NSImage.Name("status-logo")) else { return }
let resizedLogo = NSImage(size: NSSize(width: 18, height: 18), flipped: false) { (dstRect) -> Bool in
logo.draw(in: dstRect)
return true
}
然后设置button.image = resizedLogo