Programmatically changing desktop image

后端 未结 1 1872
后悔当初
后悔当初 2020-12-16 08:28

I am trying to change the desktop image; the procedure I\'ve come up with is below. The first time this code is run, the resized image is displayed on screen as wallpaper, b

相关标签:
1条回答
  • 2020-12-16 08:47

    Why not try -[NSWorkspace setDesktopImageURL:forScreen:options:error:]? Apple has a sample project called DesktopImage to give you some idea how to use it.


    Edit (after reading your code more carefully): The problem you're having may be because of your call to +[NSDictionary dictionaryWithObjectsAndKeys:] See the nil at the end of the list of arguments? That's how you tell NSDictionary that your argument list is done. You can't put nil in the list, because it will stop reading the list at that point. If you want to specify a key that has no value, you have to use [NSNull null].


    An aside: you've got a memory management issue in your code:

    // allocates memory for an NSURL
    NSURL * url = [[NSURL alloc] init]; 
    // allocates more memory for an NSURL, and leaks 
    // the earlier allocation
    url = [NSURL URLWithString:imagePath]; 
    

    Just do one or the other:

    // If you do it this way, you will have to call 
    // [url release] later
    NSURL * url = [[NSURL alloc] initWithString:imagePath];
    // This memory will be released automatically
    NSURL * otherUrl = [NSURL URLWithString:imagePath];
    
    0 讨论(0)
提交回复
热议问题