以编程方式更改桌面映像 [英] Programmatically changing desktop image

查看:82
本文介绍了以编程方式更改桌面映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改桌面图像;我想出的步骤如下.第一次运行此代码时,调整大小后的图像将在屏幕上显示为墙纸,但是下次没有反应.我在做什么错了?

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

推荐答案

为什么不尝试的示例项目. DesktopImage 让您知道如何使用它.

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

编辑(在仔细阅读代码后): 您遇到的问题可能是由于您调用了+[NSDictionary dictionaryWithObjectsAndKeys:]看到参数列表末尾的nil吗?这就是您告诉NSDictionary参数列表已完成的方式.您不能将nil放在列表中,因为它会在此时停止读取列表.如果要指定没有值的键,则必须使用[NSNull null].

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

只需做一个或另一个:

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

这篇关于以编程方式更改桌面映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆