问题
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?
回答1:
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];
回答2:
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
}
来源:https://stackoverflow.com/questions/27374355/nssavepanel-crashes-on-yosemite