Thursday, 14 March 2013

How to use Images(& other Files) in a tweak - Theos

When working with MobileSubstrate tweaks you cannot use imageNamed: unless you are looking for images in the location of the application you are hooking. So to get images into your application you must add a bundle to your tweak and make a Resources folder with the images inside it.

Makefile - Add To The Bottom of your Makefile and change NameOfYourBundle to be the name you wish.
NOTE: It cannot be the same as your TWEAK_NAME

        BUNDLE_NAME = NameOfYourBundle
        NameOfYourBundle_INSTALL_PATH = /Library/MobileSubstrate/DynamicLibraries
        include $(THEOS)/makefiles/bundle.mk

Resources - In the main directory of your tweak add a folder called Resources and place your images inside it.

Code - Create a NSBundle, init with "initWithPath:", get the image path with "pathForResource:", get image with "imageWithContentsOfFile:"
       
        #define kBundlePath @"/Library/MobileSubstrate/DynamicLibraries/NameOfYourBundle.bundle"

        //Get the Bundle
        NSBundle *bundle = [[NSBundle alloc] initWithPath:kBundlePath];
        
        //Get Path to Image
        NSString *imagePath = [bundle pathForResource:@"imageName" ofType:@"png"];

        //UIImage From File
        UIImage *image = [UIImage imageWithContentsOfFile:imageOnePath];

NOTE:The same basic idea also works for other files types, just use the appropriate classes equivalent method