为NSStatusItem图像缩放PDF图像

问题描述 投票:0回答:1

我正在尝试使用状态栏项创建一个macOS应用,该状态栏项使用PDF而不是PNG图像,但是当我使用该图像时,它的尺寸过大,整个状态项为黑色。我找不到缩放图像的方法,以使其适合其他Mac状态栏项目。

图标

  • Font Awesome
    • (我无法附上pdf,因此您需要手动转换它)

Assets.xcassets

image of scode showing the pdf icon of a globe

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

期望

A picture of the icon

现实

A screenshot showing a black square for my icon alongside other status bar items

swift macos statusbar appkit
1个回答
1
投票

您应该调整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

© www.soinside.com 2019 - 2024. All rights reserved.