#import <UIKit/UIKit.h> @interface imageSet : UIViewController { NSMutableArray *filePathArray; NSFileManager *localFileManager; }
NSDirectoryEnumerator *myDirectoryEnumerator; NSFileManager *myFileManager=[NSFileManager defaultManager]; myDirectoryEnumerator=[myFileManager enumeratorAtPath:[[NSBundle mainBundle] bundlePath]]; NSString *docPath; docPath = [self documentsPath]; //得到文件的路径 NSLog(@"%@", docPath); filePathArray = [[NSMutableArray alloc]init]; //用来存目录名字的数组 localFileManager=[[NSFileManager alloc] init]; //用于获取文件列表 NSString *file; while((file=[myDirectoryEnumerator nextObject])) //遍历当前目录 { if([[file pathExtension] isEqualToString:@"png"]) //取得后缀名这.png的文件名 { [filePathArray addObject:[docPath stringByAppendingPathComponent:file]]; //存到数组 NSLog(@"%@",file); //NSLog(@"%@",filePathArray); } }
//获得项目中当前文件名里的内容 -(NSString *)bundlePath:(NSString *)fileName { return [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:fileName]; //[NSBundle mainBundle] resourcePath } //获得项目中同一级文件中目录名字 -(NSString *)documentsPath:(NSString *)fileName { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; return [documentsDirectory stringByAppendingPathComponent:fileName]; } -(NSString *)documentsPath { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; return documentsDirectory; }
本文介绍了一种在iOS应用中加载图片资源的方法,通过遍历指定目录下的所有.png格式文件,并将其路径存储到数组中,以便于后续在应用中进行图片资源的加载和管理。

被折叠的 条评论
为什么被折叠?



