注意:
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;