2017-07-09 4 views
1

Actuellement, j'ai ceci.NSWorkspace setDesktopImageURL correspond à l'écran

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

Quand je mets mon image comme fond d'écran, les paramètres par défaut d'image à l'option « écran remplissage » lorsqu'elle est cochée dans les préférences du système. Je voudrais qu'il soit réglé sur l'option "fit to screen" - un moyen de le faire?

Répondre

0

Vous pouvez obtenir le comportement "taille pour s'adapter" en définissant NSImageScaling.scaleProportionallyUpOrDown pour la clé NSWorkspaceDesktopImageScalingKey dans le dictionnaire des options de l'écran.

Exemple 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) 
} 
+0

Hm - Je reçois un sélecteur non reconnu envoyé à l'erreur d'exemple, en utilisant ce; sans les options, tout fonctionne. Avec elle, l'erreur se produit :( – skywang329

+0

Erreur: 2017-07-11 19: 02: 52,717387 + 0800 EmpyreanEyes [70941: 14848228] - [_ SwiftValue integerValue]: sélecteur non reconnu envoyé à l'instance 0x608000240a20 2017-07-11 19: 02: 52.717883 + 0800 EmpyreanEyes [70941: 14848228] [Général] - [_ SwiftValue integerValue]: sélecteur non reconnu envoyé à l'instance 0x608000240a20 2017-07-11 19: 02: 52.720604 + 0800 EmpyreanEyes [70941: 14848228] – skywang329

+0

L'erreur semble être avec des options [NSWorkspaceDesktopImageScalingKey] = NSImageScaling.scaleProportionallyUpOrDown Lorsque je commente cela, cela fonctionne Les autres options [NSWorkspaceDesktopImageAllowClippingKey] = true fonctionne bien – skywang329