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

3 comments:

  1. Man, I am following your example, but the image is not getting loaded. I tried to inspect the contents of the /Library/MobileSubstrate/DynamicLibraries path, but the subpath MobileSubstrate/DynamicLibraries does not exists. Should I create the path by myself?
    Thanks in advance.

    ReplyDelete
  2. Yes please answer the above question. I need to know the answer too!

    ReplyDelete
    Replies
    1. Ok, so, now it works. What was my problem?: that I mistyped the extension ".bundle" and I had typed ".bund"
      Another reason was that I pasted a .jpg image in the Resources directory. In code, I was using ofType:@"jpg", while in fact, the resources compiler converts the jpg to png (o.O)
      You dont have to create the folders; theos make does it for you.

      Good luck m8.

      Delete