gpt4 book ai didi

swift - NSWorkspace setDesktopImageURL 适合屏幕

转载 作者:行者123 更新时间:2023-11-28 08:06:19 28 4
gpt4 key购买 nike

目前,我有这个。

var workspace = NSWorkspace.shared()    
do {
try workspace.setDesktopImageURL(destinationURL, for: screen, options: [:])
} catch {}

当我将我的图像设置为桌面墙纸时,在系统偏好设置中检查时图像默认为“填充屏幕”选项。我希望将其设置为“适合屏幕”选项 - 有什么方法可以做到这一点吗?

最佳答案

您可以通过为屏幕选项字典中的键 NSWorkspaceDesktopImageScalingKey 设置 NSImageScaling.scaleProportionallyUpOrDown 来获得“大小适合”行为。

Swift 3 中的示例:

do {
// here we use the first screen, adapt to your case
guard let screens = NSScreen.screens(),
let screen = screens.first else
{
// handle error if no screen is available
return
}
let workspace = NSWorkspace.shared()
// we get the screen's options dictionary in a variable
guard var options = workspace.desktopImageOptions(for: screen) else {
// handle error if no options dictionary is available for this screen
return
}
// we add (or replace) our options in the dictionary
// "size to fit" is NSImageScaling.scaleProportionallyUpOrDown
options[NSWorkspaceDesktopImageScalingKey] = NSImageScaling.scaleProportionallyUpOrDown
options[NSWorkspaceDesktopImageAllowClippingKey] = true
// finally we write the image using the new options
try workspace.setDesktopImageURL(destinationURL, for: screen, options: options)
} catch {
print(error.localizedDescription)
}

关于swift - NSWorkspace setDesktopImageURL 适合屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44999876/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com