Ios 使用应用程序中的所有图像创建阵列?

Ios 使用应用程序中的所有图像创建阵列?,ios,arrays,image,bundle,Ios,Arrays,Image,Bundle,是否可以使用我的应用程序中的所有.png、.jpg和.jpeg创建NSArray?我知道这是可能的,以某种类型,如只.pngs,但我不知道如何做到这一点,在我的应用程序中的所有图像 我宁愿没有太多的条件,那么我怎么能轻松地做到这一点呢 谢谢 编辑1: // Do any additional setup after loading the view, typically from a nib. NSString * pathToImages = [NSString stringWithForma

是否可以使用我的应用程序中的所有.png、.jpg和.jpeg创建NSArray?我知道这是可能的,以某种类型,如只.pngs,但我不知道如何做到这一点,在我的应用程序中的所有图像

我宁愿没有太多的条件,那么我怎么能轻松地做到这一点呢

谢谢

编辑1:

// Do any additional setup after loading the view, typically from a nib.
NSString * pathToImages = [NSString stringWithFormat: @"%@/images", [[NSBundle mainBundle] resourcePath]];
NSFileManager *localFileManager= [[[NSFileManager alloc] init] autorelease];
NSDirectoryEnumerator *dirEnum = [localFileManager enumeratorAtPath:pathToImages];
NSString *file;
NSLog(@"level1");
while (file = [dirEnum nextObject]) {
    NSLog(@"level2");
    if(file)
    {
        NSLog(@"level3");
        [imagesArray addObject:file];
    }
}

取决于在阵列中存储图像的方式。您想要文件引用还是实际的UIImage对象

无论如何。。。如果您将所有图像保存在应用程序资源目录中的“图像”文件夹中,为什么不尝试以下操作:

NSString * pathToImages = [NSString stringWithFormat: @"%@/images", [[NSBundle mainBundle] resourcePath]];
NSFileManager *localFileManager= [[NSFileManager alloc] init];
NSDirectoryEnumerator *dirEnum = [localFileManager enumeratorAtPath:docsDir];
NSMutableArray * arrayOfImages = [[NSMutableArray alloc] initWithCapacity: 1];
NSString *file;
while (file = [dirEnum nextObject]) {
    // let's assume that all objects in here are valid images
    UIImage * newImage = [UIImage imageWithContentsOfFile: file];
    if(newImage)
    {
        [arrayOfImages addObject: newImage];
    }
}

顺便说一下。。。如果加载大量图像(尤其是高rez图像),则可能会在视图控制器中进行调用。

如何使用NSStrings?AKA文件引用?然后只需执行
[arrayOfImages addObject:file]而不是创建新的
UIImage
对象。当例程完成后,您将拥有一个文件引用数组。我不确定您是否使用了ARC。在
addObject
行设置一个断点,看看它是否到达那里。如果数组为空,听起来好像永远不会命中。我没有使用ARC,但我会检查,因为我只是添加了NSLogs检查,我在上面展示了,它甚至没有达到2级!有什么想法吗?