Geet
Geet

Reputation: 2437

imageWithContentsOfFile Not Returning Image

UIImage *image=[UIImage imageWithContentsOfFile:filePath];

I have used this method a hundred times before and it used to always work, but now it just returns" Null", I googled and I tried with

            NSBundle *bundle = [NSBundle mainBundle];
            NSString *filePath = [bundle pathForResource:
                                   @"fileName" ofType:@"png"];
            UIImage *image=[UIImage imageWithContentsOfFile:filePath];

But its still not Working, Also tried using

UIImage *image=[UIImage imageWithData:[NSData dataWithContentsOfFile:filePath]];

still get "Null",

And I can't use the Method

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

Since I am downloading the Images inside my Apps Document Directory inside a "tmp" folder and then using the path where I have downloaded them and then trying to access it, Hence I need this method "imageWithContentsOfFile:" to work desperately

can somebody please help me out of this…Thanks for help in advance

Upvotes: 3

Views: 8334

Answers (3)

Rebecca Duhard
Rebecca Duhard

Reputation: 916

Not sure if the issue I was having is the same as yours but we were suddenly having this issue where we were getting nil for our images when using the full file path and using the imageWithContentsOfFilePath method.

We found after a bit of debugging that the NSHomeDirectory() (which is basically your sandbox location), now changes on every launch. Our issue was that we were storing the entire file path and on subsequent launches, trying to retrieve them from that same full file path, which no longer existed.

For example, on one launch your home directory might be: /var/mobile/Containers/Data/Application/844B1DDA-49AB-4B5C-AC76-2BBF1397B142 but it would be different on the next: /var/mobile/Containers/Data/Application/C620B610-3839-4134-9DF7-C5756F22BBCE. Therefore, if you store file paths for future use, you'll have to determine the home directory portion of the file path at the time you require it and simply store the relative path of the file/image.

NSString *filePath = [NSString stringWithFormat:@"%@/%@", [self retrieveHomeDirectoryPath], relativeFileLocationPath];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];

Here retrieveHomeDirectoryPath would simply return the result of NSHomeDirectory() or NSTemporaryDirectory() and relativeFileLocationPath would be the path relative to that specific location.

Upvotes: 12

poojathorat
poojathorat

Reputation: 1220

As you have to load image from Documents Directory 1st access it using below code

NSArray  *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 

    NSUserDomainMask, YES);

    NSString *documentsDir  = [documentPaths objectAtIndex:0];

    NSString  *pngfile = [documentsDir stringByAppendingPathComponent:@"fileName.png"];

    & then apply filename to imageview like

     imageView.image=[UIImage imageWithData:[NSData dataWithContentsOfFile:pngfile]];

Here the filename.png is the name of png file which you have saved in Applications Document directory.

Upvotes: 0

Cripto
Cripto

Reputation: 3751

I would guess that there is something wrong with your import. Xcode does funky things sometimes.

Right click on the image in your resources and delete and remove the reference. Reimport and make sure that you select the box that says copy.

This is how I set images.

self.someUIImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"somePictureThatExists.png"]];

Or more simply:

self.someUIImageView = [UIImage imageNamed:@"somePictureThatExists.png"];

Edit:

I would also clean the project when you run into issues like this.

Command+Shift+K or Product>Clean

Upvotes: 0

Related Questions