1

I have this NSImage I want to save to disk on a sandbox app.

I have this code:

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

    }
  }];

}

CRASH: an error occurred while attempting to connect to listener 'com.apple.view-bridge': Connection interrupted - Assertion failure in +[NSXPCSharedListener connectionForListenerNamed:fromServiceNamed:], /SourceCache/ViewBridge/ViewBridge-99/NSXPCSharedListener.m:394] NSXPCSharedListener unable to create endpoint for listener named com.apple.view-bridge

here are the entitlements:

enter image description here

I have also tried this without success.

Community
  • 1
  • 1
Duck
  • 34,902
  • 47
  • 248
  • 470
  • Make sure you call exportPNGImage: from main thread. Also why are you adding the sheet to windows[0] and not mainWindow? How do you make sure windows[0] is the correct one? – mahal tertin Jan 20 '15 at 16:03
  • the crash is on the line `NSSavePanel *panel = [NSSavePanel savePanel];`. The panel cannot be used on sandboxed apps with storyboards. Have you tried? – Duck Jan 20 '15 at 21:00

1 Answers1

1
[NSSavePanel savePanel];

and

[NSSavePanel openPanel];

simply don't work on OSX 10.10, 10.10.1 and 10.10.2 on storyboard apps.

The solution given by Apple to me was "use Xibs".

After having a long list of problems with storyboard apps on OSX, I don't use them anymore. "Use XIBS" is what I am doing.

Duck
  • 34,902
  • 47
  • 248
  • 470