objective-c - 以编程方式更改桌面图像

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

最佳答案

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

关于objective-c - 以编程方式更改桌面图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5468074/

相关文章:

objective-c - 如何在 AppDelegate 中执行 Segue?

ios - NSFetchRequest 和 NSPredicate,数组比较和匹配

ios - 添加后 GMSMarker 未显示在 map 上

macos - AppleScript as .app 不从命令行获取参数

swift - NSBezierPath 绘制方向错误

安卓工作室 : Symbol not found error

swift - 为什么我可以为 NSWindowController 的子类调用一个无参数的 init()?

ios - View 内的多个 subview 和自动调整大小

cocoa - 将自定义方法添加到子类 NSManagedObject

ios - XCode 调试器意外切换当前执行行