1

I want to get image from IImage object from specified path location.Here is the code .

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  
                                                         NSUserDomainMask,   YES);

NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:@"image1.png"];
NSLog(writablePath);
UIImage *image=[UIImage imageNamed:writablePath];

I get image object nil. so what could be the problem? I have also varified that image1.png is there at writablePath.

3 Answers3

7

I believe UIImage imageNamed looks for images from the application bundle, rather than by full path. You can use UIImage imageWithContentsOfFile to load from a full path (but it won't be cached), or just put the image in your bundle and use imageNamed.

You also need to check that the file is actually being placed in your application bundle, and is not just sitting in your project directory.

Adam Wright
  • 48,938
  • 12
  • 131
  • 152
  • Do you know if it is possible to use imageNamed: when loading images from the caches folder (NSCachesDirectory) ? – Martijn Thé Feb 21 '10 at 16:29
  • You should use imagewithContentsOfFile as it doesn't cache the image in memory. Repeated loadings with imageNamed: will drive up memory usage even after you replace or remove the image. – TigerCoding Apr 09 '11 at 11:25
  • @Javy, I dont think you mean to say that repeated loadings of the _same image_ with imageNamed: will drive up memory usage right? Because it's cached after all. Upendra, I've tried to find the same answer - it's annoying to have all the images in the main directory but there doesn't seem to be a way - imageNamed always looks in the bundle root – Rhubarb Oct 16 '12 at 07:49
  • Loading many different images drive up memory usage. If you loaded 10 images in view controller one, then 10 different images from another view controller, they would all be cached. imageNamed is useful if you're loading the same images over and over. Either way, the cache is cleared of unused image data when memory is used up, but I think it's good practice to cache only when reusing the same image often. – TigerCoding Oct 17 '12 at 10:40
0

The exact answer to your question is that you are getting nil because you're using imageNamed: which looks for the image using the given (path) name relative to the bundle root (i.e. the .app directory in the deployed app), whereas you are giving it an absolute path to the document directory.

You can actually use imageNamed to load images from a subdirectory of your bundle. This requires two things:

  1. that your images are actually in a subdirectory in your deployed bundle, not just a subdirectory or group in your XCode project - since those get collapsed into the root directory. In other words, you need a "blue group" in XCode (folder reference).

  2. You use a relative path in imageNamed.

So [UIImageView imageNamed:@"subdir/foo"] will load foo.png or foo@2x.png, where these files appear in a "blue" group in XCode called "subdir".

You could use this to also find the image in a subdirectory of your documents, since you know where Documents will be created relative to your bundle, but that's a bit hacky. For loading images from the documents you should use imageWithContentsOfFile: instead of imageNamed:

I got sick of looking for the answer to this kind of thing and did some testing, and detailed this and the rest of the results (with pictures!) in an answer to this question:

UIImage imageNamed requires pathForResource?

Community
  • 1
  • 1
Rhubarb
  • 34,705
  • 2
  • 49
  • 38
0

I think use this:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  
                                                         NSUserDomainMask,   YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:@"image1.png"];
NSLog(writablePath);

===insted of above write this===

UIImage *image=[UIImage imageWithContentsOfFile:writablePath];

Scar
  • 3,460
  • 3
  • 26
  • 51
Satish Azad
  • 2,302
  • 1
  • 16
  • 35