如何获取所有图像名称?
我想获取一个文件夹中的所有图像名称。
我想要做的是,我把所有的图像放在一个名为“文件”
现在是可以将所有图像名称添加到数组,所以我不需要手动写入所有的图像名称数组或有任何其他简单的方法来做到这一点?
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];
- 从命令行terminal创build带有位代码启用和位代码禁用的设备和通用configuration的iOS框架
- 如何在不使用核心位置服务的情况下获取用户的当前城市名称?
- MPMediaQuery和iTunes Match的问题
- 在为ARM64构build时,架构x86_64的未定义符号
- IMEI作为UDID的后备,这是一个好主意吗?
- 如何删除我的itunesconnect应用程序的状态“准备提交”?
- 我如何自学iOS 11,Swift 4和Metal2 API,ARKit,并在2Weeks中制作了原型。
- 快速有哪些可选项?
- 如何添加图像在iOS 6的UITabBarController的'更多'button?