以编程方式更改桌面映像

时间:2011-03-29 04:58:20

标签: objective-c macos cocoa desktop

我正在尝试更改桌面图片;我提出的程序如下。第一次运行此代码时,调整大小的图像在屏幕上显示为壁纸,但下次没有反应。我做错了什么?

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

1 个答案:

答案 0 :(得分:5)

为什么不试试-[NSWorkspace setDesktopImageURL:forScreen:options:error:]? Apple有一个名为DesktopImage的示例项目,可以让您了解如何使用它。


修改(仔细阅读您的代码后): 您遇到的问题可能是因为您致电+[NSDictionary dictionaryWithObjectsAndKeys:]请参阅参数列表末尾的nil?这就是你告诉NSDictionary你的参数列表完成的方法。您不能将nil放入列表中,因为它将在此时停止读取列表。如果要指定没有值的键,则必须使用[NSNull null]


暂且不说:您的代码中存在内存管理问题:

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

只做一个或另一个:

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