以编程方式更改桌面图像

发布于 2024-10-27 05:25:46 字数 2936 浏览 0 评论 0原文

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终止放荡 2024-11-03 05:25:46

为什么不尝试 -[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];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文