objective c - NSSavePanel File Path -
i use nssavepanel when deal text files. if have image export, use nsopenpanel user can select directory, , don't caught in sandbox file path restriction. time, though, want use nssavepanel let user save image file (bmp, gif, jpeg, jp2, png).
- (void)exportfile { nsstring *documentfolderpath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) lastobject]; nssavepanel *panel = [nssavepanel savepanel]; [panel setmessage:@"please select path save checkboard image."]; // message inside modal window if ([self fileexists:destinationpath]) { [panel setdirectoryurl:[nsurl fileurlwithpath:destinationpath]]; } else { [panel setdirectoryurl:[nsurl fileurlwithpath:documentfolderpath]]; } //[panel setallowedfiletypes:[[nsarray alloc] initwithobjects:@"bmp",@"gif",@"jpg",@"jp2",@"png",nil]]; [panel setallowsotherfiletypes:yes]; [panel setextensionhidden:yes]; [panel setcancreatedirectories:yes]; [panel setnamefieldstringvalue:filename]; [panel settitle:@"saving checkboard..."]; // window title [panel setaccessoryview:accessoryview1]; nsinteger result = [panel runmodal]; nserror *error = nil; if (result == nsokbutton) { //////////////////////////////////////////// nsstring *path0 = [[panel url] path]; nsmutablestring *path1 = [[nsmutablestring alloc] initwithstring:path0]; if (formatindex1 == 0) { // formatindex1 index of nspopmenu [path1 appendstring:@".bmp"]; } else if (formatindex1 == 1) { [path1 appendstring:@".gif"]; } else if (formatindex1 == 2) { [path1 appendstring:@".jpg"]; } else if (formatindex1 == 3) { [path1 appendstring:@".jp2"]; } else if (formatindex1 == 4) { [path1 appendstring:@".png"]; } [self exportfile2:path1]; // <<<<<<<<<<<<<<< immediate goal //////////////////////////////////////////// if (error) { [nsapp presenterror:error]; } } }
this nssavepanel has accessoryview control, allowing user select graphic format nspopupmenu. index formatindex1. know code fails if application sandboxed. that's because exact path user selects path0, , code above appends file extension before reaching exportfile2. how go getting exact path file extension. preview automatically appends file extension if 'hide extension' off. yet, preview won't fail export image.
thank help.
try below:-
- (void)exportfile { nssavepanel *panel = [nssavepanel savepanel]; nsstring *filename=nil; if (formatindex1 == 0) { // formatindex1 index of nspopmenu filename=@"untitle.bmp" } else if (formatindex1 == 1) { filename=@"untitle.gif" } else if (formatindex1 == 2) { filename=@"untitle.jpg" } [panel setmessage:@"please select path save checkboard image."]; // message inside modal window [panel setallowsotherfiletypes:yes]; [panel setextensionhidden:yes]; [panel setcancreatedirectories:yes]; [panel setnamefieldstringvalue:filename]; [panel settitle:@"saving checkboard..."]; // window title [panel setaccessoryview:accessoryview1]; nsinteger result = [panel runmodal]; nserror *error = nil; if (result == nsokbutton) { //////////////////////////////////////////// nsstring *path0 = [[panel url] path]; [self exportfile2:path0]; // <<<<<<<<<<<<<<< immediate goal //////////////////////////////////////////// if (error) { [nsapp presenterror:error]; } } }
Comments
Post a Comment