[英]NSWorkspace setDesktopImageURL fit to screen
目前,我有这个。
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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.