Tom Tallak Solbu
Tom Tallak Solbu

Reputation: 559

How do I extract images from bundle with specific names and add to array,

In my app bundle, I have several images of several items.

ItemA_largepicture.png

ItemA_smallPicture.png

ItemA_overViewPicture.png

ItemB_largepicture.png

ItemB_smallPicture.png

ItemB_overViewPicture.png

ItemC_largepicture.png

ItemC_smallPicture.png

ItemC_overViewPicture.png

...

I want to extract, for example all ItemB pictures,to display in scrollView. I can easily get one by doing following

    path=[ItemB stringByAppendingString:@"_largePicture];
    NSString *imagePath=[[NSBundle mainBundle]pathForResource:path ofType:@"png"];
    UIImage *image=[UIImage imageWithContentsOfFile:imagePath];
    [self.image setImage:image];

So the question is simply; how do I get all the ItemB pictures? Do I need to put them into an array? and how do I do that?

Upvotes: 0

Views: 930

Answers (2)

Paresh Navadiya
Paresh Navadiya

Reputation: 38249

this code should list everything in your mainBundle

NSString *bundleRootPath = [[NSBundle mainBundle] bundlePath];
NSArray *bundleRootContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundleRootPath error:nil];
NSArray *files = [bundleRootContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self beginswith 'ItemA' OR self beginswith 'ItemB' OR self beginswith 'ItemC'"]]; //you can provide your string in ' ' to filter specific content
NSLog(@"%@",files);

Upvotes: 1

Bink
Bink

Reputation: 171

largePicturePath=[ItemB stringByAppendingString:@"_largePicture];
smallPicturePath=[ItemB stringByAppendingString:@"_smallPicture];
overViewPicturePath=[ItemB stringByAppendingString:@"_overViewPicture];
NSString *largeImagePath=[[NSBundle mainBundle]pathForResource:largePicturePath ofType:@"png"];
NSString *smallImagePath=[[NSBundle mainBundle]pathForResource:smallPicturePath ofType:@"png"];
NSString *overViewImagePath=[[NSBundle mainBundle]pathForResource:overViewPicturePath ofType:@"png"];

NSArray *imageArray = [[NSArray arrayWithObjects:largeImagePath,smallImagePath,overViewImagePath,nil];

NSEnumerator *e = [imageArray objectEnumerator];

NSString *path;
while (path= [e nextObject]) {
    UIImage *image=[UIImage imageWithContentsOfFile:path];

    ...

}

Upvotes: 1

Related Questions