objective c - Display NSImage on a CALayer -
i've been trying display nsimage on calayer. realised need convert cgimage apparently, display it...
i have code doesn't seem working
calayer *layer = [calayer layer]; nsimage *findericon = [[nsworkspace sharedworkspace] iconforfiletype:nsfiletypeforhfstypecode(kfindericon)]; [findericon setsize:(nssize){ 128.0f, 128.0f }]; cgimagesourceref source; source = cgimagesourcecreatewithdata((cfdataref)findericon, null); cgimageref finalicon = cgimagesourcecreateimageatindex(source, 0, null); layer.bounds = cgrectmake(128.0f, 128.0f, 4, 4); layer.position = cgpointmake(128.0f, 128.0f); layer.contents = finalicon; // insert layer root layer [mainlayer addsublayer:layer];
why? how can work?
from comments: actually, if you're on 10.6, can set calayer's contents nsimage rather cgimageref...
if you're on os x 10.6 or later, take @ nsimage
's cgimageforproposedrect:context:hints:
method.
if you're not, i've got in category on nsimage
:
-(cgimageref)cgimage { cgcontextref bitmapctx = cgbitmapcontextcreate(null/*data - pass null let cg allocate memory*/, [self size].width, [self size].height, 8 /*bitspercomponent*/, 0 /*bytesperrow - cg calculate if it's allocating data. might padded out bit better alignment*/, [[nscolorspace genericrgbcolorspace] cgcolorspace], kcgbitmapbyteorder32host|kcgimagealphapremultipliedfirst); [nsgraphicscontext savegraphicsstate]; [nsgraphicscontext setcurrentcontext:[nsgraphicscontext graphicscontextwithgraphicsport:bitmapctx flipped:no]]; [self drawinrect:nsmakerect(0,0, [self size].width, [self size].height) fromrect:nszerorect operation:nscompositecopy fraction:1.0]; [nsgraphicscontext restoregraphicsstate]; cgimageref cgimage = cgbitmapcontextcreateimage(bitmapctx); cgcontextrelease(bitmapctx); return (cgimageref)[(id)cgimage autorelease]; }
i think wrote myself. it's entirely possible ripped off somewhere else stack overflow. it's older personal project , don't remember.
Comments
Post a Comment