【问题标题】:Why is NSFileManager unable to find these png files?为什么 NSFileManager 找不到这些 png 文件?
【发布时间】:2017-07-14 11:29:40
【问题描述】:

在研究了此处找到的问题(abcdefg)的答案后,我编写了打开图像文件的代码。但是即使我将 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.

__

【问题讨论】:

    标签: ios objective-c uiimageview nsfilemanager


    【解决方案1】:

    图片不在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"];
    

    【讨论】:

    • Subramanian,进步。 iTunesArtwork1024.png 出现在目录中,但图像仍然不显示。查看我的编辑
    • Subramanian,您已经解释并解决了我的问题。非常感激。这必须是公认的答案。
    【解决方案2】:

    您可以访问项目中添加的名称的图像。试试下面的代码

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

    【讨论】:

      【解决方案3】:

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

      要加载存储在 XCode 项目中的图像,只需执行以下操作:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        • 2022-01-05
        • 1970-01-01
        相关资源
        最近更新 更多