NSSavePanel crashes on Yosemite

657 views Asked by At

I am using NSSavePanel to save image.

I have used IKSaveOption which gets added to the NSSavePanel. When save panel tries to open the sheet for window it crashes saying -

*** Assertion failure in -[IKSaveOptionsContainer _didChangeHostsAutolayoutEngineTo:], /SourceCache/AppKit/AppKit-1343.14/Layout.subproj/NSView_Layout.m:577 - Should translate autoresizing mask into constraints if _didChangeHostsAutolayoutEngineTo:YES.

I am following this code:

NSSavePanel *savePanel = [NSSavePanel savePanel];
    [savePanel setDirectoryURL:[NSURL URLWithString:NSHomeDirectory()]];
    [savePanel setDelegate:self];
    [savePanel setPrompt:NSLocalizedString(@"save",nil)];
    [savePanel setAllowedFileTypes:[NSArray  arrayWithObjects:@"png",@"jpeg",nil]];
      IKSaveOptions * opt = [[IKSaveOptions alloc] initWithImageProperties:nil
                                                          imageUTType:(NSString *)kUTTypePNG];
    [opt addSaveOptionsAccessoryViewToSavePanel:savePanel];
    [savePanel setExtensionHidden:NO];

   [savePanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result){.....
}

This code works in Maverick, but not on Yosemite. Are there any layout changes in the new OS API?

2

There are 2 answers

0
Marek H On

El Capitan + Yosemite workaround is to avoid adding it with addSaveOptionsAccessoryViewToSavePanel but use addSaveOptionsToView + manually update extension using allowedFileTypes

var saveOptions = IKSaveOptions()
var imageUTType = kUTTypeTIFF
var imageProperties: NSDictionary = Dictionary<String, String>()
var panel : NSSavePanel?

internal func prepareExportSavePanel(savePanel : NSSavePanel) -> Bool {
    panel = savePanel
    saveOptions = IKSaveOptions(imageProperties: imageProperties , imageUTType: imageUTType)
    saveOptions.delegate = self
    if #available(macOS 10.14, *) { //might work on earlier versions
        saveOptions.addAccessoryView(to: savePanel)
    } else {
        savePanel.accessoryView = NSView(frame: NSRect(x: 0, y: 0, width: 500, height: 200))
        saveOptions.add(to: savePanel.accessoryView)
    }

    return true
}

override func saveOptions(saveOptions: IKSaveOptions!, shouldShowUTType utType: String!) -> Bool {
    if (utType == "com.ilm.openexr-image") {
        return false
    }
    return true
}

@objc dynamic func saveOptionsChanged(_ sender: Any?) {
    imageProperties = saveOptions.imageProperties
    imageUTType = saveOptions.imageUTType
    panel?.allowedFileTypes = [imageUTType as String]
}

@objc dynamic open var canCalculateEstimatedSize : Bool {
    return false
}
0
Jason On

Update2: The solution turns out to be simple.

[self.saveOptions addSaveOptionsAccessoryViewToSavePanel:savePanel];
savePanel.accessoryView.translatesAutoresizingMaskIntoConstraints = YES;

Update: The below workaround does not change the filename's extension and depending on how you save the file, the new format request may not be honored.

I found a workaround, and I filed a bug with Apple (20595916). The workaround isn't great because the superview's size might be inadequate and clip the controls for the image options.

NSView* view = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 400, 200)];
view.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
savePanel.accessoryView = view;

[self.saveOptions addSaveOptionsToView:view];