Search code examples
iostitanium

Add image to Titanium iOS module


Seems like the official documents are out of date and none of its solution works for using image in Titanium iOS module.

According to http://docs.appcelerator.com/platform/latest/#!/guide/iOS_Module_Project, I should place images in assets directory and use relative path to get it in Objective C code. I did what it said but the image is null.

I located the actual application bundle and found out the image is in modules/moduleid/xxx.png, but the code just can't load it.


Solution

  • Instead following official guidelines like (which does not work apparently):

    NSString *path = [NSString stringWithFormat:@"modules/%@/foo.png",[self moduleId]];
    NSURL *url = [TiUtils toURL:path proxy:self];
    UIImage *image = [TiUtils image:url proxy:self];
    

    Do this:

    NSString *imageName = [NSString stringWithFormat:@"modules/%@/foo.png",[self moduleId]];    
    UIImage *image = [UIImage imageNamed:imageName];
    

    In most cases, this code will be in custom views, which means the moduleId would be hard coded.