2011-04-21 8 views
3

내 Mac OS 응용 프로그램에서 새 폴더를 만들라는 메시지가 나타납니다. Cocoa를 만들 때이 폴더에 아이콘을 적용하고 싶습니다. 현재, 나는 다음과 같은 코드를 사용하고 폴더를 만들 :Cocoa를 사용하여 폴더 아이콘 만들기

bool set = [[NSWorkspace sharedWorkspace] setIcon:[NSImage imageNamed:@"icon.icns"] forFile:path options:NSExcludeQuickDrawElementsIconCreationOption]; 

동안 : 디렉토리를 선택한 후

- (IBAction)browseFiles:(id)sender 
{ 
    NSOpenPanel *oPanel = [[NSOpenPanel openPanel] retain]; 
    [oPanel setCanChooseDirectories:YES]; 
    [oPanel setCanChooseFiles:NO]; 
    [oPanel setDelegate:self]; 
    [oPanel setCanCreateDirectories:YES]; 
    [oPanel beginSheetForDirectory:NSHomeDirectory() 
           file:nil 
          types:nil 
        modalForWindow:nil 
        modalDelegate:self 
        didEndSelector:@selector(filePanelDidEnd: 
              returnCode: 
              contextInfo:) 
         contextInfo:nil]; 
} 

을, 사용자는 다음과 같은 방법으로 함수를 호출하는 확인 버튼을 클릭 위 코드가 "예"를 반환하면 아이콘이 폴더에 성공적으로 적용되지 않습니다. 내 코드에서 뭔가 잘못하고 있니?

감사합니다.

+0

여기 찾고 시도해보십시오 http://stackoverflow.com/questions/475410/what-are-the-dimensions-file-types-and-ppi-of- 아이폰 아이콘. 아이콘에 PNG 파일이 필요할 수도 있습니다. – Shankar

+3

이 질문은 프로그래밍 방식으로 Mac OS X 아이콘을 설정하는 것에 관한 것입니다. 링크 된 대답은 iPhone 아이콘의 형식을 설명합니다. –

답변

5

NSWorkspace 방법은 여기의 매력처럼 작동합니다. 아이콘이 잘못된 형식 일 수 있습니까?
내가 Finder 아이콘을 사용하여 setIcon:을 시도 :

- (IBAction)setFolderIcon:(id)sender 
{ 
    NSOpenPanel* openPanel = [NSOpenPanel openPanel]; 
    [openPanel setCanChooseFiles:NO]; 
    [openPanel setCanChooseDirectories:YES]; 
    switch([openPanel runModal]) 
    { 
     case NSFileHandlingPanelOKButton: 
     { 
      NSURL* directoryURL = [openPanel directoryURL]; 
      NSImage* iconImage = [[NSImage alloc] initWithContentsOfFile:@"/System/Library/CoreServices/Finder.app/Contents/Resources/Finder.icns"]; 
      BOOL didSetIcon = [[NSWorkspace sharedWorkspace] setIcon:iconImage forFile:[directoryURL path] options:0]; 
      NSLog(@"%d", didSetIcon); 
      [iconImage release]; 
     } 
     case NSFileHandlingPanelCancelButton: 
     { 
      return; 
     } 
    } 
} 
+1

GC를 사용하지 않는다고 가정하고 NSImage 인스턴스를 해제하는 것을 잊지 마십시오. –

+0

피터에게 감사드립니다. 업데이트 된 답변. –