How to embed an image in application's source code ?

Monday, March 13 2006 @ 12:34 PM UTC

Contributed by: cocoa

If you don't want the image to be accessible in the application bundle or if the image has to be in a command line tool (without bundle): how do you embed an image in the source code - kind of static NSData ?

Download here a small command line tool (Universal) that let's you convert any file content into a char buffer.


file2ctsr < myimage > myimage.c
file2ctsr < myimage > myimage.m
file2ctsr -p variableName myImage >  myimage.cpp
are typical usages.
The image.X source file will contain something like:

char *buffer0 = "\376\355\372\316\000\000\000\022\000\000\000\000\000\000\000\002\000\000\000\012\000\000\005\104\000\000\000\205\000\000\000\001"
"\000\000\000\070\137\137\120\101\107\105\132\105\122\117\000\000\000\000\000\000\000\000\000\000\000\000\020\000\000\000\000\000"
"\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\004\000\000\000\001\000\000\001\214\137\137\124\105"
...
"\162\157\162\137\155\145\163\163\141\147\145\000\144\171\154\144\137\146\165\156\143\137\154\157\157\153\165\160\137\160\157\151"
"\156\164\145\162\000\137\144\141\162\167\151\156\137\165\156\167\151\156\144\137\144\171\154\144\137\141\144\144\137\151\155\141"
"\147\145\137\150\157\157\153\000\137\144\141\162\167\151\156\137\165\156\167\151\156\144\137\144\171\154\144\137\162\145\155\157"
"\166\145\137\151\155\141\147\145\137\150\157\157\153\000\137\157\143\164\141\154\056\062\071\070\070\000\137\146\165\156\143\160"
"\164\162\000\000"
	;
size_t buffer0Len = 18052 ;

If you want to use it as the data source for an image in a Cocoa application:

NSImage *myImage = [[NSImage alloc] initWithData:[NSData dataWithBytes:buffer0 length:buffer0Len]] ;

1 comments



http://www.macosxguru.net/article.php?story=20060313123432657