如何从具有特定名称的包中提取图像并添加到数组,

时间:2012-09-18 18:10:37

标签: objective-c xcode bundle

在我的应用包中,我有几个项目的几张图片。

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图片? 我需要将它们放入阵列吗?我该怎么做?

2 个答案:

答案 0 :(得分: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];

    ...

}

答案 1 :(得分: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);