【问题标题】:Programmatically changing desktop image以编程方式更改桌面图像
【发布时间】:2011-07-24 12:38:16
【问题描述】:

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

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

【问题讨论】:

    标签: objective-c macos cocoa desktop


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-18
      • 1970-01-01
      • 2021-01-14
      • 1970-01-01
      • 1970-01-01
      • 2019-07-07
      • 1970-01-01
      相关资源
      最近更新 更多