objective c - Share CGColor between iOS and OSX -
i'm writing core graphics cross plattform library osx , ios.
i'm porting on existing ios project created uicolor cgcolorattribute. of course uicolor not available on mac.
so whats right way use same color on both systems? should rgb value of used uicolors , create cgcolors programmtically or should divide code target_os_mac , target_os_iphone ifdefs?
my preferred approach avoid uicolor , nscolor altogether, , use cgcolorref wherever possible.
core animation best/most modern way draw on both ios , os x. expects cgcolorref objects , (pretty much) cross platform.
the drawback is, being lower level data type, arc doesn't handle memory management you. manual memory management isn't hard.
usage of cgcolorref:
cgcolorspaceref space = cgcolorspacecreatedevicergb(); cgfloat componennts[4] = {255, 255, 255, 255}; // white, 100% opacity. use {0,0,0,255} black, or {255,0,0,127.5} red 50% opacity. cgcolorref color = cgcolorcreate(colorspace, componennts); cgcolorspacerelease(space); layer.backgroundcolor = color; // layer calayer object cgcolorrelease(color); you avoid creating/releasing color space object storing in static variable:
static cgcolorspaceref colorspace; @implementation myclass + (void)initialize { static dispatch_once_t oncetoken; dispatch_once(&oncetoken, ^{ colorspace = cgcolorspacecreatedevicergb(); }); } - (void)blah { cgfloat componennts[4] = {255, 255, 255, 255}; // white, 100% opacity. use {0,0,0,255} black, or {255,0,0,127.5} red 50% opacity. cgcolorref color = cgcolorcreate(space, componennts); layer.backgroundcolor = color; // layer calayer object cgcolorrelease(color);
Comments
Post a Comment