首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSWorkspace setDesktopImageURL适应屏幕

NSWorkspace setDesktopImageURL适应屏幕
EN

Stack Overflow用户
提问于 2017-07-10 02:52:27
回答 1查看 465关注 0票数 1

目前,我有这个。

代码语言:javascript
复制
var workspace = NSWorkspace.shared()    
do {
    try workspace.setDesktopImageURL(destinationURL, for: screen, options: [:])
} catch {}

当我将我的图像设置为桌面墙纸时,当在系统首选项中选中时,图像默认为"fill screen“选项。我希望将其设置为“适应屏幕”选项-有什么方法可以做到吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-10 04:03:17

您可以通过在屏幕的选项字典中为key NSWorkspaceDesktopImageScalingKey设置NSImageScaling.scaleProportionallyUpOrDown来获得"size to fit“行为。

Swift 3中的示例:

代码语言:javascript
复制
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)
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44999876

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档