0

I have written code to open image files after studying answers to the questions found here (a, b, c, d, e, f & g). But NSFileManager is unable to find them even though I added the png files to the project. I'm reasonably confident my code should be able to recognise either of the png files if they were in the right directory.

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];

}

But here is the log in the debug 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

Here is the state of the project after two png files were added. enter image description here Yet the log shows they are not visible to NSFileManager. So where would they be found ? i.e. what changes do I need to make to my code in order to find them ?


EDIT

This draft finds the png file following Subramanian's recommendation.

    - (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];
}

The log now reads

     Found file path :  … .app/iTunesArtwork1024.png 

and the image also appears in the subview.

__

Community
  • 1
  • 1
Greg
  • 1,750
  • 2
  • 29
  • 56

3 Answers3

2

Image is not in theDocument Directory, It's inside the bundle.

You have added the image files inside the project. But You are checking the image on Document Directory, Which is wrong. Image is inside app bundle.

You can simply assign the Image to UIImageView by the name of the image.

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

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

If you want to get the path of the image, then you have to use [NSBundle mainBundle]

[[NSBundle mainBundle]pathForResource:@"iTunesArtwork1024" ofType:@"png"];
Subramanian P
  • 4,365
  • 2
  • 21
  • 25
  • Subramanian, progress. iTunesArtwork1024.png appears in the directory but the image still doesn't display. See my edit – Greg Jul 14 '17 at 12:24
  • Subramanian, you've explained and solved my problem. Much appreciated. This has to be the accepted answer. – Greg Jul 14 '17 at 12:36
1

You can access images with names which are added in projects. Try with below code

 UIImage  *image = [UIImage imageNamed:@"iTunesArtwork1024.png"];
Narayana Rao Routhu
  • 6,303
  • 27
  • 42
1

The path where your are looking for the image is inside your device (real or simulator).

To load an image that is stored in your XCode project just do:

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

Miguel Isla
  • 1,379
  • 14
  • 25