objective c - Exporting unshown NSViewController to JPG -
i working on desktop app generates image en nsviewcontroller not shown. view generated has 3 nsimageviews
change content once created.
i manage export nsviewcontroller's
view jpg file, never renders in image given nsimageview
, keeps image set in nib. also, if don't set background color view's layer... empty result.
what missing here? guess need update nsviewcontroller's view... how can that? not want show nsviewcontroller.
my code:
- (ibaction)doexport:(id)sender { exportview *view = [[exportview alloc] initwithnibname:@"exportview" bundle:nil]; [view.pic1 setimage:self.pic1.image]; [view.pic2 setimage:self.pic2.image]; [view.pic3 setimage:self.pic3.image]; [view.icon setimage:self.icon.image]; // hardcoding test [view.pic1 setimage:[nsimage imagenamed:@"iphone 4-inch screenshot 1"]]; [view.pic1 setneedslayout:yes]; [view.pic1 setneedsdisplay]; [view.pic1 updatelayer]; // setup image render nsrect imgrect = view.view.frame; nssize imgsize = imgrect.size; nsbitmapimagerep *rep = [[nsbitmapimagerep alloc] initwithbitmapdataplanes:null pixelswide:imgsize.width pixelshigh:imgsize.height bitspersample:8 samplesperpixel:4 hasalpha:yes isplanar:no colorspacename:nsdevicergbcolorspace bitmapformat:nsalphafirstbitmapformat bytesperrow:0 bitsperpixel:0]; nsgraphicscontext *g = [nsgraphicscontext graphicscontextwithbitmapimagerep:rep]; [nsgraphicscontext savegraphicsstate]; [nsgraphicscontext setcurrentcontext:g]; // set view background color calayer *viewlayer = [calayer layer]; [viewlayer setbackgroundcolor:[nscolor clearcolor].cgcolor]; [view.view setwantslayer:yes]; [view.view setlayer:viewlayer]; // render cgcontextref zcgcontextref = (cgcontextref) [g graphicsport]; [[view.view layer] renderincontext:zcgcontextref]; // render pic well, test [view.pic1.layer renderincontext:zcgcontextref]; // data image nsdata *exporteddata = [rep representationusingtype:nsjpegfiletype properties:nil]; // start savepanel nssavepanel *savepanel = [nssavepanel savepanel]; savepanel.title = @"save chart"; [savepanel setallowedfiletypes:[nsarray arraywithobject:@"jpg"]]; [savepanel beginsheetmodalforwindow:self.window completionhandler:^(nsinteger result) { if (nsfilehandlingpanelokbutton == result) { nsurl* fileurl = [savepanel url]; if ([fileurl.pathextension isequaltostring:@""]) fileurl = [fileurl urlbyappendingpathextension:@"jpg"]; [exporteddata writetourl:fileurl atomically:yes]; } }]; }
i solved in ugly way. add view contentview
, set sethidden
yes
straight away. intialize view. change content of nib view. when time render data nsdata
change sethidden
no
, generate nsdata
, remove view.
the end result (no beautiful code :( ):
- (ibaction)doexport:(id)sender { exportview *view = [[exportview alloc] initwithnibname:@"exportview" bundle:nil]; [view.view sethidden:yes]; [self.window.contentview addsubview:view.view]; [view.pic1 setimage:self.pic1.image]; [view.pic2 setimage:self.pic2.image]; [view.pic3 setimage:self.pic3.image]; [view.icon setimage:self.icon.image]; // setup image render nsrect imgrect = view.view.frame; nssize imgsize = imgrect.size; nsbitmapimagerep *rep = [[nsbitmapimagerep alloc] initwithbitmapdataplanes:null pixelswide:imgsize.width pixelshigh:imgsize.height bitspersample:8 samplesperpixel:4 hasalpha:yes isplanar:no colorspacename:nsdevicergbcolorspace bitmapformat:nsalphafirstbitmapformat bytesperrow:0 bitsperpixel:0]; nsgraphicscontext *g = [nsgraphicscontext graphicscontextwithbitmapimagerep:rep]; [nsgraphicscontext savegraphicsstate]; [nsgraphicscontext setcurrentcontext:g]; // set view background color calayer *viewlayer = [calayer layer]; [viewlayer setbackgroundcolor:[nscolor clearcolor].cgcolor]; [view.view setwantslayer:yes]; [view.view setlayer:viewlayer]; [view.view sethidden:no]; // render cgcontextref zcgcontextref = (cgcontextref) [g graphicsport]; [[view.view layer] renderincontext:zcgcontextref]; [view.view removefromsuperview]; // data image nsdata *exporteddata = [rep representationusingtype:nsjpegfiletype properties:nil]; // start savepanel nssavepanel *savepanel = [nssavepanel savepanel]; savepanel.title = @"save chart"; [savepanel setallowedfiletypes:[nsarray arraywithobject:@"jpg"]]; [savepanel setnamefieldstringvalue:@"test"]; [savepanel beginsheetmodalforwindow:self.window completionhandler:^(nsinteger result) { if (nsfilehandlingpanelokbutton == result) { nsurl* fileurl = [savepanel url]; if ([fileurl.pathextension isequaltostring:@""]) fileurl = [fileurl urlbyappendingpathextension:@"jpg"]; [exporteddata writetourl:fileurl atomically:yes]; } }]; }
Comments
Post a Comment