【发布时间】:2015-01-18 22:31:22
【问题描述】:
我有这个 NSImage 我想保存到沙盒应用程序的磁盘。
我有这个代码:
- (void)exportPNGImage:(NSImage *)image withName:(NSString*)name
{
NSArray *windows =[[NSApplication sharedApplication] windows];
NSWindow *window = windows[0];
// Build a new name for the file using the current name and
// the filename extension associated with the specified UTI.
CFStringRef newExtension = UTTypeCopyPreferredTagWithClass(kUTTypePNG,
kUTTagClassFilenameExtension);
NSString* newName = [[name stringByDeletingPathExtension]
stringByAppendingPathExtension:(__bridge NSString*)newExtension];
NSSavePanel *panel = [NSSavePanel savePanel];
[panel setNameFieldStringValue:newName];
[panel setAllowsOtherFileTypes:NO];
[panel setAllowedFileTypes:@[(__bridge NSString*)newExtension]];
[panel beginSheetModalForWindow:window completionHandler:^(NSInteger result){
if (result == NSFileHandlingPanelOKButton)
{
NSURL *fileURL = [panel URL];
// Write the contents in the new format.
NSBitmapImageRep *imgRep = [[image representations] objectAtIndex: 0];
NSData *data = [imgRep representationUsingType: NSPNGFileType properties: nil];
[data writeToURL:fileURL atomically:YES];
}
}];
}
崩溃:尝试连接到侦听器“com.apple.view-bridge”时发生错误:连接中断 - +[NSXPCSharedListener connectionForListenerNamed:fromServiceNamed:] 中的断言失败,/SourceCache/ViewBridge/ViewBridge-99 /NSXPCSharedListener.m:394] NSXPCSharedListener 无法为名为 com.apple.view-bridge 的侦听器创建端点
以下是权利:
我也试过this,但没有成功。
【问题讨论】:
-
确保从主线程调用 exportPNGImage:。另外,为什么要将工作表添加到 windows[0] 而不是 mainWindow?你如何确保 windows[0] 是正确的?
-
崩溃在线
NSSavePanel *panel = [NSSavePanel savePanel];。该面板不能用于带有情节提要的沙盒应用程序。你试过了吗?
标签: macos cocoa sandbox entitlements nssavepanel