我已經寫代碼學習的答案在這裏發現了問題之後打開圖像文件(a,b,c,d,e,f & g)。但即使我將png文件添加到項目中,NSFileManager也無法找到它們。我相當有信心我的代碼應該能夠識別任何一個png文件,如果它們在正確的目錄中。爲什麼NSFileManager無法找到這些png文件?
e.g.
- (void)viewDidLoad {
[super viewDidLoad];
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSLog(@"\ndirPaths %@ \n\ndocsDir \n%@", dirPaths, docsDir);
NSString *imageFilePath = [docsDir stringByAppendingPathComponent:@"iTunesArtwork1024.png"];
NSLog(@"\n\nfile path should be \n\n%@ \n\n", imageFilePath);
NSData *imageData = [NSData dataWithContentsOfFile:imageFilePath];
if ([fileManager fileExistsAtPath:imageFilePath])
{
NSLog(@"\nFound file path : %@", imageFilePath);
}
else
{
NSLog(@"\nImage file not found");
}
UIImage *image = [UIImage imageWithData:imageData];
UIImageView *logo = [[UIImageView alloc] init];
logo.image = image;
[self.view addSubview:logo];
}
但這裏是日誌中的調試窗口
2017-07-14 18:26:35.679 IconShape[1089:348564]
dirPaths (
"/Users/gs/Library/Developer/CoreSimulator/Devices/57279C80-0937-4658-B0E6-7984B3768D56/data/Containers/Data/Application/18235DBF-7ADB-47D4-AFF9-282D02F2A0F8/Documents"
)
docsDir
/Users/gs/Library/Developer/CoreSimulator/Devices/57279C80-0937-4658-B0E6-7984B3768D56/data/Containers/Data/Application/18235DBF-7ADB-47D4-AFF9-282D02F2A0F8/Documents
2017-07-14 18:26:35.679 IconShape[1089:348564]
file path should be
/Users/gs/Library/Developer/CoreSimulator/Devices/57279C80-0937-4658-B0E6-7984B3768D56/data/Containers/Data/Application/18235DBF-7ADB-47D4-AFF9-282D02F2A0F8/Documents/iTunesArtwork1024.png
2017-07-14 18:26:35.679 IconShape[1089:348564]
Image file not found
這裏是增加了兩個PNG文件後,該項目的狀態。 但是日誌顯示它們對NSFileManager不可見。那麼他們會在哪裏找到?即我需要對代碼進行哪些更改才能找到它們?
編輯
該草案認爲以下薩勃拉曼尼亞的建議PNG文件。
- (void)viewDidLoad {
[super viewDidLoad];
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString *imageFilePath = [[NSBundle mainBundle]pathForResource:@"iTunesArtwork1024" ofType:@"png"];
if ([fileManager fileExistsAtPath:imageFilePath])
{
NSLog(@"\nFound file path : %@", imageFilePath);
}
else
{
NSLog(@"\nImage file not found");
}
UIImage *image = [UIImage imageNamed:@"iTunesArtwork1024.png"];
UIImageView *logo = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 50, 50)];
logo.image = image;
[self.view addSubview:logo];
}
日誌現在讀取
Found file path : … .app/iTunesArtwork1024.png
和形象也出現在subview.
__
薩勃拉曼尼亞,進步。 iTunesArtwork1024.png出現在目錄中,但圖像仍然不顯示。查看我的編輯 – Greg
Subramanian,你已經解釋並解決了我的問題。非常感激。這必須是被接受的答案。 – Greg