Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
510 views
in Technique[技术] by (71.8m points)

objective c - Programmatically changing desktop image

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, but the next time there is no reaction. What am I doing wrong?

-(IBAction)click:(id)sender
{
    NSData *sourceData;
    NSError *error;
    NSFileManager *filemgr;
    filemgr = [NSFileManager defaultManager];
    screenArray = [NSScreen screens];
    screenCount = [screenArray count];
    unsigned index  = 0;

    for (index; index < screenCount; index++)
    {
        screenz = [screenArray objectAtIndex: index];
        screenRect = [screenz visibleFrame];

    }
    NSLog(@"%fx%f",screenRect.size.width, screenRect.size.height);

    arrCatDetails = [strCatDetails  componentsSeparatedByString:appDelegate.strColDelimiter];

    NSString *imageURL = [NSString stringWithFormat:@"upload/product/image/%@_%@_%d.jpg",[arrCatDetails objectAtIndex:0],appDelegate.str104by157Name,iSelectedImgIndex];
    NSString *ima = [imageURL lastPathComponent];
    NSString *str = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *dataFilePath = [str stringByAppendingPathComponent:ima];
    NSString *imagePath = [NSString stringWithFormat:@"file://localhost%@",dataFilePath];
    NSURL *url = [[NSURL alloc] init];
    url = [NSURL URLWithString:imagePath];
    sourceData =  [NSData dataWithContentsOfURL:url];  
    sourceImage = [[NSImage alloc] initWithData: sourceData];
    resizedImage = [[NSImage alloc] initWithSize: NSMakeSize(screenRect.size.width, screenRect.size.height)];
    NSSize originalSize = [sourceImage size];
    [resizedImage lockFocus];
    [sourceImage drawInRect: NSMakeRect(0, 0, screenRect.size.width, screenRect.size.height) fromRect: NSMakeRect(0, 0, originalSize.width, originalSize.height) operation: NSCompositeSourceOver fraction: 1.0];
    [resizedImage unlockFocus];
    NSData *resizedData = [resizedImage TIFFRepresentation];
    NSBitmapImageRep* theImageRepresentation = [NSBitmapImageRep imageRepWithData:resizedData];
    newimage = @"editwall.jpg";
    newFilePath = [str stringByAppendingPathComponent:newimage];
    NSData* theImageData = [theImageRepresentation representationUsingType:NSJPEGFileType properties:nil];
    [theImageData writeToFile: newFilePath atomically: YES];

    if([filemgr fileExistsAtPath:newFilePath] == YES)
    {   
        imagePath1 = [NSString stringWithFormat:@"file://localhost%@",newFilePath];

        urlz = [NSURL URLWithString:imagePath1];

        NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:nil, NSWorkspaceDesktopImageFillColorKey, [NSNumber numberWithBool:NO], NSWorkspaceDesktopImageAllowClippingKey, [NSNumber numberWithInteger:NSImageScaleProportionallyUpOrDown], NSWorkspaceDesktopImageScalingKey, nil];

        [[NSWorkspace sharedWorkspace] setDesktopImageURL:urlz forScreen:[[NSScreen screens] lastObject]  options:options error:&error];

    }
    else 
    {
        NSLog(@"No");
    }

    [sourceImage release];
    [resizedImage release];
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...