Ios 为什么NSFileManager找不到这些png文件?
在研究了这里(、、&)的问题答案后,我编写了打开图像文件的代码。但是,即使我将png文件添加到项目中,NSFileManager也无法找到它们。我有理由相信,如果png文件位于正确的目录中,我的代码应该能够识别它们Ios 为什么NSFileManager找不到这些png文件?,ios,objective-c,uiimageview,nsfilemanager,Ios,Objective C,Uiimageview,Nsfilemanager,在研究了这里(、、&)的问题答案后,我编写了打开图像文件的代码。但是,即使我将png文件添加到项目中,NSFileManager也无法找到它们。我有理由相信,如果png文件位于正确的目录中,我的代码应该能够识别它们 e.g. - (void)viewDidLoad { [super viewDidLoad]; NSFileManager *fileManager = [[NSFileManager alloc] init]; NSArray *dirPaths;
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看不到它们。那么在哪里可以找到它们呢?i、 e.我需要对代码进行哪些更改才能找到它们
编辑 该草案根据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
图像也会出现在子视图中。
__图像不在
文档目录中,它在包中
UIImage *image = [UIImage imageNamed:@"iTunesArtwork1024.png"];
UIImageView *logo = [[UIImageView alloc] init];
logo.image = 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"];
UIImageView *logo = [[UIImageView alloc] init];
logo.image = 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"];
您可以使用项目中添加的名称访问图像。尝试使用以下代码
UIImage *image = [UIImage imageNamed:@"iTunesArtwork1024.png"];
您查找图像的路径位于设备内部(real或simulator)
要加载存储在XCode项目中的映像,只需执行以下操作:
UIImage*image=[UIImage imagename:@“iTunesArtwork1024.png”]代码>查找图像的路径位于设备内部(real或simulator)
要加载存储在XCode项目中的映像,只需执行以下操作:
UIImage*image=[UIImage imagename:@“iTunesArtwork1024.png”]代码>Subramanian,进度。iTunesArtwork1024.png将显示在目录中,但图像仍不显示。看看我的编辑Subramanian,你已经解释并解决了我的问题。非常感谢。这是公认的答案。苏布拉曼尼安,进步。iTunesArtwork1024.png将显示在目录中,但图像仍不显示。看看我的编辑Subramanian,你已经解释并解决了我的问题。非常感谢。这必须是公认的答案。