[英]How do I save file to temp directory from file browser
我正在使用此方法打开文件浏览器并仅搜索图像文件。
NSOpenPanel* openPanel = [NSOpenPanel openPanel];
NSArray* imageTypes = [NSImage imageTypes];
[openPanel setCanChooseFiles:YES];
[openPanel setCanChooseDirectories:NO];
[openPanel setAllowsMultipleSelection:NO];
[openPanel setResolvesAliases:YES];
[openPanel setAllowedFileTypes:imageTypes];
[openPanel setPrompt:@"Upload"];
[openPanel setDirectoryURL:[NSURL fileURLWithPath:NSHomeDirectory()]];
[openPanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
[openPanel close];
单击“上传”按钮时,对话框关闭。 如何将打开的图像以特定名称保存到临时目录? 谢谢
NSOpenPanel
类不执行任何类型的文件处理。 为了获取用户选择的文件,可以使用其URLs
方法,然后使用NSTemporaryDirectory()
将该URL处的文件复制到目标NSTemporaryDirectory()
。 这是(未经测试的)代码示例:
NSOpenPanel* openPanel = [NSOpenPanel openPanel];
NSArray* imageTypes = [NSImage imageTypes];
[openPanel setCanChooseFiles:YES];
[openPanel setCanChooseDirectories:NO];
[openPanel setAllowsMultipleSelection:NO];
[openPanel setResolvesAliases:YES];
[openPanel setAllowedFileTypes:imageTypes];
[openPanel setPrompt:@"Upload"];
[openPanel setDirectoryURL:[NSURL fileURLWithPath:NSHomeDirectory()]];
[openPanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
if (result != NSOKButton) {
return;
}
NSString *myString = [[[openPanel URLs] objectAtIndex:0] absoluteString];
NSError *error = nil;
[[NSFileManager defaultManager] copyItemAtPath:myString toPath:NSTemporaryDirectory() error:&error];
if(error) {
// Error! Tell the user by showing an NSAlert, etc.
}
}];
这非常简单,如果您在网上搜索“如何使用Cocoa复制文件”,那么您将找到大量答案。 与如何访问NSOpenPanel
返回的文件NSOpenPanel
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.