2017-07-14 39 views
0

我已经写代码学习的答案在这里发现了问题之后打开图像文件(abcdef & 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文件后,该项目的状态。 enter image description here 但是日志显示它们对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.

__

回答

2

图片是不是在Document Directory,这是bundle内。

您已经在项目中添加了图像文件。但是您正在检查Document目录上的图像,这是错误的。图片在app bundle之内。

您可以简单地通过图像的nameImage分配给UIImageView

UIImage *image = [UIImage imageNamed:@"iTunesArtwork1024.png"]; 

UIImageView *logo = [[UIImageView alloc] init]; 
logo.image = image; 

如果你想要得到的图片的路径,那么你必须使用[NSBundle mainBundle]

[[NSBundle mainBundle]pathForResource:@"iTunesArtwork1024" ofType:@"png"]; 
+0

萨勃拉曼尼亚,进步。 iTunesArtwork1024.png出现在目录中,但图像仍然不显示。查看我的编辑 – Greg

+0

Subramanian,你已经解释并解决了我的问题。非常感激。这必须是被接受的答案。 – Greg

1

你可以用它们在项目中添加名称访问图像。下面的代码尝试

UIImage *image = [UIImage imageNamed:@"iTunesArtwork1024.png"]; 
1

在您正在寻找的图像的路径是设备内部的(真实或模拟)。

要加载存储在您的Xcode项目中的图像只是做:

UIImage* image = [UIImage imageNamed:@"iTunesArtwork1024.png"];

相关问题