我已经写代码学习的答案在这里发现了问题之后打开图像文件(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