2

I have 400 images with repeating names, hierarchically divided among folders which i have included in my project, now i want to load images with the absolute path, I tried using imagenamed but instead of images being load, empty box is showed. Now I need a little help in how can i show images using absolute path.

Hierarchy of my project

Project
     -> Project Files (i-e App Deligate and view controllers)
     -> Resources
            ->Images
                  ->1
                      ->1.jpg
                      ->2.jpg
                      ->3.jpg
                  ->2
                      ->1.jpg
                      ->2.jpg
                      ->3.jpg

Method i am using to load images

[self.leftImage setImage:[UIImage imageNamed:@"Images/1/1.jpg"] forState:UIControlStateNormal];

and so on... Any one, help. Regards

X-Factor
  • 2,067
  • 14
  • 18
  • 2
    You cannot use same named images in XCode project. Because at the build creation, XCode creates a single folder (aka main bundle) to store all its resources. It doesnot refer to manually created folders (like 1, 2 in your example). So over there same named images gets replaced. Use separate names to all image files. – Mrunal Mar 12 '13 at 11:11
  • @mrunal i have 400 images, so i can't rename them all ? can you suggest me a solution to fetch the image using absolute path or something like that ? – X-Factor Mar 12 '13 at 11:21
  • Sorry but its rule set by apple itself. For better way, download some file rename manager and rename all those files. In your code, you might require to change by yourself. – Mrunal Mar 12 '13 at 11:32

1 Answers1

2

As per your Comments

The following method may help you

- (NSString *)pathForResource:(NSString *)name ofType:(NSString *)ext inDirectory:(NSString *)subpath;
- (NSString *)pathForResource:(NSString *)name ofType:(NSString *)ext inDirectory:(NSString *)subpath forLocalization:(NSString *)localizationName;

- (NSArray *)pathsForResourcesOfType:(NSString *)ext inDirectory:(NSString *)subpath;
- (NSArray *)pathsForResourcesOfType:(NSString *)ext inDirectory:(NSString *)subpath forLocalization:(NSString *)localizationName;

You can try this code

NSString * sample=[[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"1"] ofType:@".jpg" inDirectory:@"foldername"];
[self.leftImage setImage:[UIImage imageWithContentsOfFile:sample] forState:UIControlStateNormal];

And also you cant have 2 images of same name. You will get a duplicate symbols warning while building ur app.

For memory efficient use pathforresource type rather than image named for UIImage because it will have some cache memory on ViewControllers.

Hope it helps !!!

Vishnu
  • 2,243
  • 2
  • 21
  • 44