2011-03-29 90 views
2

我正在嘗試更改桌面圖像;我提出的程序如下。該代碼第一次運行時,調整大小的圖像作爲牆紙顯示在屏幕上,但下一次沒有反應。我究竟做錯了什麼?以編程方式更改桌面圖像

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

回答

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