0

我只在处理文本文件时使用NSSavePanel 。如果我有要导出的图像,我使用NSOpenPanel以便用户可以选择一个目录,并且我不会陷入沙箱文件路径限制。不过,这一次,我想使用 NSSavePanel 让用户保存图像文件(bmp、gif、jpeg、jp2、png)。

- (void)exportfile {    
    NSString *documentFolderPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSSavePanel *panel = [NSSavePanel savePanel];

    [panel setMessage:@"Please select a path where to save checkboard as an image."]; // Message inside modal window
    if ([self fileExists:destinationpath]) {
        [panel setDirectoryURL:[NSURL fileURLWithPath:destinationpath]];
    } else {
        [panel setDirectoryURL:[NSURL fileURLWithPath:documentFolderPath]];
    }

    //[panel setAllowedFileTypes:[[NSArray alloc] initWithObjects:@"bmp",@"gif",@"jpg",@"jp2",@"png",nil]];
    [panel setAllowsOtherFileTypes:YES];
    [panel setExtensionHidden:YES];
    [panel setCanCreateDirectories:YES];
    [panel setNameFieldStringValue:filename];
    [panel setTitle:@"Saving checkboard..."]; // Window title
    [panel setAccessoryView:accessoryView1];

    NSInteger result = [panel runModal];
    NSError *error = nil;

    if (result == NSOKButton) {     
        ////////////////////////////////////////////
        NSString *path0 = [[panel URL] path];
        NSMutableString *path1 = [[NSMutableString alloc] initWithString:path0];
        if (formatIndex1 == 0) { // formatIndex1 is the index of NSPopMenu
            [path1 appendString:@".bmp"];
        }
        else if (formatIndex1 == 1) {
            [path1 appendString:@".gif"];
        }
        else if (formatIndex1 == 2) {
            [path1 appendString:@".jpg"];
        }
        else if (formatIndex1 == 3) {
            [path1 appendString:@".jp2"];
        }
        else if (formatIndex1 == 4) {
            [path1 appendString:@".png"];
        }
        [self exportfile2:path1]; // <<<<<<<<<<<<<<< Immediate goal
        ////////////////////////////////////////////

        if (error) {
            [NSApp presentError:error];
        }
    }
}

这个 NSSavePanel 有一个 AccessoryView 控件,允许用户使用NSPopupMenu选择图形格式。它的索引是formatIndex1。我知道如果应用程序被沙盒化,代码会失败。这是因为用户选择的确切路径是 path0,并且上面的代码在到达 exportfile2 之前为其附加了文件扩展名。那么我该如何获取带有文件扩展名的确切路径。 如果“隐藏扩展名”关闭,预览会自动附加文件扩展名。然而,预览不会无法导出图像。

谢谢您的帮助。

4

1 回答 1

2

试试下面这样: -

- (void)exportfile {    
    NSSavePanel *panel = [NSSavePanel savePanel];
NSString *fileName=nil;

        if (formatIndex1 == 0) { // formatIndex1 is the index of NSPopMenu
        fileName=@"untitle.bmp"
        }
        else if (formatIndex1 == 1) {
        fileName=@"untitle.gif"
        }
        else if (formatIndex1 == 2) {
          fileName=@"untitle.jpg"
        }

[panel setMessage:@"Please select a path where to save checkboard as an image."]; // Message inside modal window
    [panel setAllowsOtherFileTypes:YES];
    [panel setExtensionHidden:YES];
    [panel setCanCreateDirectories:YES];
    [panel setNameFieldStringValue:filename];
    [panel setTitle:@"Saving checkboard..."]; // Window title
    [panel setAccessoryView:accessoryView1];

    NSInteger result = [panel runModal];
    NSError *error = nil;

    if (result == NSOKButton) {     
        ////////////////////////////////////////////
        NSString *path0 = [[panel URL] path];


        [self exportfile2:path0]; // <<<<<<<<<<<<<<< Immediate goal
        ////////////////////////////////////////////

        if (error) {
            [NSApp presentError:error];
        }
    }
}
于 2013-08-26T17:07:10.407 回答