在研究了此处的问题答案后,我编写了打开图像文件的代码(a,b,c,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];
}
但这是调试窗口中的日志
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.
__
答案 0 :(得分:2)
图片不在Document Directory
内,位于bundle
内。
您已在项目中添加了图像文件。但是您正在检查Document
目录上的图像,这是错误的。图片位于app bundle
内。
您只需将Image
分配给图片的UIImageView
name
即可。
UIImage *image = [UIImage imageNamed:@"iTunesArtwork1024.png"];
UIImageView *logo = [[UIImageView alloc] init];
logo.image = image;
如果您想获取图片的路径,则必须使用[NSBundle mainBundle]
[[NSBundle mainBundle]pathForResource:@"iTunesArtwork1024" ofType:@"png"];
答案 1 :(得分:1)
您可以访问在项目中添加名称的图像。尝试使用以下代码
// Call inside View controller C
self.presentingViewController?.dismissViewControllerAnimated(false, completion: nil)
self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
答案 2 :(得分:1)
您正在寻找图像的路径位于您的设备内(真实或模拟器)。
要加载存储在XCode项目中的图像,请执行以下操作:
UIImage* image = [UIImage imageNamed:@"iTunesArtwork1024.png"];