5

我有带有附件视图的NSSavePanel ,让用户选择一种图形格式,以便他们可以将图像 (NSImage) 保存为文件。到目前为止,我有以下内容。(我跳过了一些行以使其简短。)

- (void)exportFile {    
    NSString *filename;
    if (formatIndex1 == 0) { // Default selection by user in Preferences
        filename = @"Untitled.bmp";
    }
    else if (formatIndex1 == 1) {
        filename = @"Untitled.gif";
    }
    ...

    [panel setAllowedFileTypes:[[NSArray alloc] initWithObjects:@"bmp",@"gif",@"jpg",@"jp2",@"png",nil]];
    [panel setAllowsOtherFileTypes:NO];
    [panel setExtensionHidden:NO];
    [panel setCanCreateDirectories:YES];
    [panel setNameFieldStringValue:filename];
    [panel setAccessoryView:accessoryView1];
    [formatMenu1 setAction:@selector(dropMenuChange:)]; // formatMenu1 is NSPopUpButton
    [formatMenu1 setTarget:self];

    [panel beginSheetModalForWindow:window completionHandler:^(NSInteger result) {
        if (result == NSFileHandlingPanelOKButton) {
            // getting panel url
        }
    }];
}

-(void)dropMenuChange:(NSPopUpButton *)sender { 
    NSSavePanel *savePanel = (NSSavePanel *)[sender window];
    [savePanel setNameFieldStringValue:@"..."];
}

我不是 100% 确定我做对了。我想要实现的是,每当用户在附件视图的 NSPopUpButton 上选择文件格式时,我想将正确的扩展名附加到当前文件名。有没有一种神奇的方法可以做到这一点?或者我是否必须以编程方式为自己设置具有正确扩展名的当前文件名setNameFieldStringValue

谢谢您的帮助。

4

2 回答 2

16

我想要实现的是,每当用户在附件视图的 NSPopUpButton 上选择文件格式时,我想将正确的扩展名附加到当前文件名。有没有一种神奇的方法可以做到这一点?

就在这里。你不需要自己做setNameFieldStringValue:,让 savePanel 做。让我们假设fileName是一个完整的路径/Users/hg/Pictures/2013/08/Airplanes/pic123.png,它存在一个用于 savePanel 的附件视图,带有matrix of radio buttons. 每个按钮都有一个标题,如@"jpg" 或@"png" 或... 矩阵的作用是-selectFileType:

- (IBAction) selectFileType:(id)sender
{
    [savePanel setAllowedFileTypes:@[ [[sender selectedCell] title] ] ];
    // this will set the right extension
}

为了使用 savePanel,我尝试了以下代码:

- (void) saveImage:(NSImage *) theImg
{
  savePanel = [NSSavePanel savePanel];
  NSString *imageName = [fileName lastPathComponent];
  NSString *suffix = [imageName pathExtension];
  NSString *baseName = [imageName stringByDeletingPathExtension];

  // prepare the savePanel
  [savePanel setAccessoryView:accessoryView];
  [savePanel setAllowedFileTypes:@[ suffix ] ];
  [savePanel setDirectoryURL:[NSURL fileURLWithPath:fileName]];  // convert to URL
  [savePanel setNameFieldStringValue:baseName ];  // without extension !
  // savePanel does append the suffix

  // and now start the savePanel and choose the wanted fileType
  int rtn = [savePanel runModal];   // preferred method since 10.6
  if( rtn==NSFileHandlingPanelCancelButton) return;    // do nothing

  // finally create and save the file
  if( [[[savePanel allowedFileTypes] objectAtIndex:0] isEqualToString:@"jpg" ){
      // save as jpg-file
  }
  // check for other fileTypes
  . . . 
}
于 2013-08-30T08:40:00.953 回答
0

您必须使用正确的扩展名设置当前文件名 setNameFieldStringValue

-(void)dropMenuChange:(NSPopUpButton *)sender { 
    NSSavePanel *savePanel = (NSSavePanel *)[sender window];
    NSString *nameFieldString = [savePanel nameFieldStringValue];
    NSString *nameFieldStringWithExt = [NSString stringWithFormat:@"%@.%@",[savePanel nameFieldStringValue], popupvalue];
    [savePanel setNameFieldStringValue:nameFieldStringWithExt];
}
于 2013-08-26T14:27:27.573 回答