注意:
NSBundle
is used to access resources within your application itself: that is, everything inside YourApp.app. The documentsDirectory
is a location outside of your app -- it's in the "home directory" which is part of your app sandbox, and which is analogous to your user home directory on the Mac. 1.这个是获取[[NSBundle mainBundle] bundlePath]]的文件
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])) //遍历当前目录
{
// NSLog(@"%@",[file lastPathComponent]);
if ([[file lastPathComponent] isEqualToString:@"cover.jpg"]) {
[_filePathArray addObject:[docPath stringByAppendingPathComponent:file]]; //存到数组
// NSLog(@"%@",file);
NSLog(@"%@",_filePathArray);
}
// if([[file pathExtension] isEqualToString:@"json"]) //取得后缀名这.png的文件名
// {
//
// }
}
2.这个是获取沙盒里面的文件
NSDirectoryEnumerator *myDirectoryEnumerator;
NSFileManager *myFileManager=[NSFileManager defaultManager];
myDirectoryEnumerator=[myFileManager enumeratorAtPath:NSHomeDirectory()];
NSString *docPath;
docPath = [self documentsPath]; //得到文件的路径
NSLog(@"%@", docPath);
_filePathArray = [[NSMutableArray alloc] init]; //用来存目录名字的数组
_JSONPathArray = [NSMutableArray array];
localFileManager=[[NSFileManager alloc] init]; //用于获取文件列表
NSString *file;
while((file=[myDirectoryEnumerator nextObject])) //遍历当前目录
{
// NSLog(@"%@",[file lastPathComponent]);
if ([[file lastPathComponent] isEqualToString:@"cover.jpg"]) {
[_filePathArray addObject:[docPath stringByAppendingPathComponent:file]]; //存到数组
// NSLog(@"%@",file);
NSLog(@"%@",_filePathArray);
}
if([[file lastPathComponent] isEqualToString:@"target.json"]) //取得后缀名这.json的文件名
{
[_JSONPathArray addObject:[docPath stringByAppendingPathComponent:file]];
NSLog(@"%@",_JSONPathArray);
}
}
NSString *imagePath = _filePathArray[indexPath.item];
获取在真机bundle里面的文件夹里的图片,不能用全路径
NSArray *arr = [imagePath componentsSeparatedByString:@"/Documents/"];
NSString *strSubStringDigNum = [arr objectAtIndex:1];
UIImage *imageLocal = [UIImage imageNamed:strSubStringDigNum];
下面是获取在真机沙盒里面的文件夹里的图片,要用全路径,而且不能用 imageNamed:imagePath
如果用imageNamed:imagePath 会出现只能获取第一个文件夹的图片
UIImage *imageDownload = [UIImage imageWithContentsOfFile:imagePath];
cell.image = imageLocal ? imageLocal : imageDownload;