Question

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

Était-ce utile?

La solution

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 !!!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top