Skip to content

How to "hide" pictures in your framework

Reid Ellis edited this page May 31, 2013 · 2 revisions

kstenerud just helped me out a lot with an issue with the header files of the framework, and how to import them, so I figured I should do my part and help with something.


So why "hide" pictures in your framework?

If you want to create a framework and let others use it with images as resources, the images will be open to other users for editing. This means they might replace them, and you might not want someone to replace your logo for example.


Use Base64 to hide your pictures.

You can translate any picture to Base64 which is basically a string.

You can use this site to do this:

www.motobit.com/util/base64-decoder-encoder.asp

After you chose a file and convert it to string you can use it in your code. For example:

#define kBase64_Orange \
@"R0lGODlhMAAwAPf/ANfJuKldI7SgkJNOGoFDFsp1KsFuJNuCLtKCO/elPdF9M66HZ8x6M86YZuOL\
M8VwJd2KPIZ2adaldpybm3M7E31BFZRSIqOjo+OVRLBqNsl9QaxgJPm8Wry8vHpzbfzDXMVyLGNZ\
U4NFGMLCwlZMJum1dapcHltQR/KtVNyMQblyO9qEM9HR0dTOxeWRPGk1EMp0JvGcOvSsTlstDfzJ\
ZOycQ+yjTuHCmmUzEE84KP7DU6plMvWxVKRZHvKiQmIyD3dhUr  ..."

Important: see that I added \ at the end of each line so that xcode would ignore new lines, that is how I can create multi-line define (and there are a lot of lines ion Base64 string).


But how do you turn a Base64 string into an image?

Well it's easy - we just convert it into NSData, and then create an image with that NSData.

For that we need the NSData category NSData+Base64. I don't have code for this category that I can share, but it should be easy to find code online. For example, there is this one. I haven't used it myself, so I hope it works for you.

Important: when I first tried to use the NSData+Base64, it didn't work because the linker did not link the category, and I got Unrecognized selector error.

To solve it, I had to add the flags:

-ObjC
-all_load

to the Other Linker Flags field in the Build Setting tab of the Target properties of the Client (the Application that uses the framework). It only works on the client, so you can't add it to the Framework when building it.

Once I had this I used this code to convert the Base64 string to NSData:

-(void) setImagesByBase64
{
    NSData *orangeData = [NSData dataFromBase64:kBase64_Orange];
    UIImage *orangeImage = [UIImage imageWithData:orangeData];
    self.orangeIV.image = orangeImage;
}

You might need another method than "dataFromBase64" in order to convert to NSData, depending on the version of NSDate+Base64 you use. In the version I gave you as link, they use dataFromBase64String

And you can guess that orangeIV is an image view on which I'm placing the image.

In order to avoid placement and scaling of the ImageView from the code, I first do it in the nib file. I place it and scale it (I use the shortcut '⌘=' after selecting the image view, and click it again, in order to match the ImageView to the size of image). Then I remove the image from the image view, and delete it from the project, and just use the Base64 as I showed in the code.


I hope this wiki entry helped you.

If you have any comments, questions or something to contribute please do so. Maybe you can even write your own wiki page here, for an issue you encountered.

10x again kstenerud for your excellent work on this project.