如何获取所有图像名称?

我想获取一个文件夹中的所有图像名称。

我想要做的是,我把所有的图像放在一个名为“文件”

现在是可以将所有图像名称添加到数组,所以我不需要手动写入所有的图像名称数组或有任何其他简单的方法来做到这一点?

NSFileManager *filemgr; NSArray *dirlist; path= [NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] bundlePath],@"Folder_Name"]; filemgr =[NSFileManager defaultManager]; dirlist = [filemgr contentsOfDirectoryAtPath:path error:NULL]; NSString *directory =[NSString stringWithFormat:@"%@/%@",path,[dirlist objectAtIndex: index]] ; NSLog(@"Path:%@",directory); NSArray *temp = [filemgr contentsOfDirectoryAtPath:directory error:NULL]; 

注意:当您在项目中添加图像文件夹时,请记得select“创build文件夹参考”选项。

使用这个完整的图像path:

  imagesUrls = [[NSMutableArray alloc]init]; for(int j =0; j< [temp count]; j++) { [imagesUrls addObject:[NSString stringWithFormat:@"%@/%@",directory,[temp objectAtIndex:j]]]; } 

最后使用imagesUrls数组来获取图像的path。

 imageView.image =[UIImage imageWithContentsOfFile:[imagesUrls objectAtIndex:i]]; 

尝试这个

 NSFileManager *filemanager = [NSFileManager defaultManager]; NSArray *filelist= [filemanager contentsOfDirectoryAtPath:filePath error:&error]; 

这里filePath是你的“Files”文件夹的path,上面的方法返回给定目录中的文件名的数组。

除了赛义夫的答案,你可以用谓词过滤结果

 NSPredicate *filter = [NSPredicate predicateWithFormat:@"self endswith '.jpg'"]; NSArray *jpgImages = [filelist filteredArrayUsingPredicate:filter];