【问题标题】:Saving a NSImage to disk on a sandbox app in cocoa - CRASH在可可的沙盒应用程序上将 NSImage 保存到磁盘 - 崩溃
【发布时间】: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


【解决方案1】:
[NSSavePanel savePanel];

[NSSavePanel openPanel];

根本无法在故事板应用程序上的 OSX 10.10、10.10.1 和 10.10.2 上运行。

Apple 给我的解决方案是“使用 Xibs”。

在 OSX 上的故事板应用程序出现一长串问题后,我不再使用它们。 “使用 XIBS”是我正在做的事情。

【讨论】:

  • 你是如何使用 XIBS 的?
  • 你不能再在 Xcode 9 上这样做了。你必须得到一个旧版本的 Xcode,可能是 Xcode 7,并在那里使用 XIB 开始一个新项目......或者手动创建它......跨度>
猜你喜欢
  • 2017-02-16
  • 2010-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-04
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
相关资源
最近更新 更多