为什么 NSFileManager 找不到这些 png 文件?
Why is NSFileManager unable to find these png files?
在研究了此处 (a, b, , d, e, f & g) 问题的答案后,我编写了打开图像文件的代码。但是即使我将 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];
}
但是这里是调试日志window
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 不可见。那么在哪里可以找到它们呢?即我需要对我的代码进行哪些更改才能找到它们?
编辑
本草稿根据 Subramanian 的推荐找到了 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.
__
图片不在Document Directory
,在bundle
.
里面
您已经在项目中添加了图像文件。但是您正在检查 Document
目录中的图像,这是错误的。图片在 app bundle
.
内
您可以简单地通过图像的name
将Image
分配给UIImageView
。
UIImage *image = [UIImage imageNamed:@"iTunesArtwork1024.png"];
UIImageView *logo = [[UIImageView alloc] init];
logo.image = image;
如果要获取图片的路径,那就得用[NSBundle mainBundle]
[[NSBundle mainBundle]pathForResource:@"iTunesArtwork1024" ofType:@"png"];
您可以访问在项目中添加名称的图像。试试下面的代码
UIImage *image = [UIImage imageNamed:@"iTunesArtwork1024.png"];
您要查找图像的路径在您的设备(真实设备或模拟器)中。
要加载存储在您的 XCode 项目中的图像,只需执行以下操作:
UIImage* image = [UIImage imageNamed:@"iTunesArtwork1024.png"];
在研究了此处 (a, b,
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];
}
但是这里是调试日志window
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 文件后项目的状态。
编辑
本草稿根据 Subramanian 的推荐找到了 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.
__
图片不在Document Directory
,在bundle
.
您已经在项目中添加了图像文件。但是您正在检查 Document
目录中的图像,这是错误的。图片在 app bundle
.
您可以简单地通过图像的name
将Image
分配给UIImageView
。
UIImage *image = [UIImage imageNamed:@"iTunesArtwork1024.png"];
UIImageView *logo = [[UIImageView alloc] init];
logo.image = image;
如果要获取图片的路径,那就得用[NSBundle mainBundle]
[[NSBundle mainBundle]pathForResource:@"iTunesArtwork1024" ofType:@"png"];
您可以访问在项目中添加名称的图像。试试下面的代码
UIImage *image = [UIImage imageNamed:@"iTunesArtwork1024.png"];
您要查找图像的路径在您的设备(真实设备或模拟器)中。
要加载存储在您的 XCode 项目中的图像,只需执行以下操作:
UIImage* image = [UIImage imageNamed:@"iTunesArtwork1024.png"];