0

在我的应用程序包中,我有几个项目的几个图像。

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

...

我想提取,例如所有 ItemB 图片,以显示在 scrollView 中。我可以通过以下操作轻松获得

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

所以问题很简单;如何获取所有 ItemB 图片?我需要将它们放入数组中吗?我该怎么做?

4

2 回答 2

1
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];

    ...

}
于 2012-09-18T18:42:08.613 回答
1

此代码应列出您的 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);
于 2012-09-18T19:39:40.480 回答