Skip to content

Instantly share code, notes, and snippets.

@priore
Created October 25, 2013 23:50
Show Gist options
  • Save priore/7163594 to your computer and use it in GitHub Desktop.
Save priore/7163594 to your computer and use it in GitHub Desktop.
Convert picture in to Objctive-C source code
# Convert picture in to Objctive-C source code.
# for example, to include an image in a static library.
# original post : http://t.co/JqZmPyg4 @adnanced
#
# First step :
# 1. Create a text file named pic2code.sh, and save it in your Documents folder.
# 2. In the content of new text file, write script below:
#!/bin/sh
echo "// Byte array generated from file $1"
echo "// date generated: `date`"
echo
echo "unsigned long $2_size=`stat -f %z $1`;"
echo "unsigned char $2[] = {"
od -t x1 $1 | sed "s/^[0-9]\{1,\} *//g" | sed "s/ *$//g" | sed "s/$/,/g" |
sed "s/ \{1,\}/, 0x/g" | sed "s/^/0x/g" | sed "s/0x,//g"
echo "};"
# Second step :
# 1. From Automator (use spotlight if dont'have in Application/Utility) create a new workflow.
# 2. Add the finder item "Get Selected Items"
# 3. Add a "Run Shell Script" item.
# 4. In the Shell Script item, choose to pass input "as arguments", choose a Shell of "/bin/bash", and use the script below as the command text:
for f in "$@"
do
y=${f/\/*\//}
~/Documents/pic2code.sh "$f" "${y/.*/}" > "${f%.*}.c"
done
# Last step :
# 1. include the .c file in your project.
# 2. where want to use it, declare external variables:
#
# extern unsigned long mypicname_size;
# extern unsigned char mypicname[];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment