Contribute  :  Calendar  :  Advanced Search  :  Site Statistics  :  Directory  :  Files to download  :  Links for developers  :  Privacy policy  :  Web Resources  :  Polls  
    MacOSX Guru Site Information, tips and tricks, FAQ for Mac OS X developers    
 Welcome to MacOSX Guru Site
 Monday, January 22 2018 @ 06:08 AM UTC

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

   
Objective-CIf 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]] ;
 

What's Related

Story Options

How to embed an image in application's source code ? | 1 comments | Create New Account
The following comments are owned by whomever posted them. This site is not responsible for what they say.
How to embed an image in application's source code ?
Authored by: Admin on Monday, March 13 2006 @ 06:22 PM UTC
xxd -include PATH_TO_IMAGE
is another solution.
 Copyright © 2018 MacOSX Guru Site
 All trademarks and copyrights on this page are owned by their respective owners.
Powered By Geeklog 
Created this page in 0.05 seconds