gpt4 book ai didi

objective-c - NSSavePanel 文件路径

转载 作者:行者123 更新时间:2023-12-03 17:40:08 24 4
gpt4 key购买 nike

我仅在处理文本文件时使用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 之前附加了一个文件扩展名。那么我该如何获取带有文件扩展名的确切路径。如果“隐藏扩展名”关闭,预览会自动附加文件扩展名。但是,预览不会无法导出图像。

感谢您的帮助。

最佳答案

尝试如下:-

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

关于objective-c - NSSavePanel 文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18437168/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com