您可以通过在屏幕的选项字典中为键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)
}
2条答案
按热度按时间qv7cva1a1#
您可以通过在屏幕的选项字典中为键
NSWorkspaceDesktopImageScalingKey
设置NSImageScaling.scaleProportionallyUpOrDown
来获得“适合大小”行为。Swift 3中的示例:
yuvru6vn2#
出于可见性目的,.imageScaling需要一个NSNumber。
它看起来是这样的:
@Eric Aya评论