Gricha
Gricha

Reputation: 1032

iOS - UIImage imageNamed: returns null on device

[UIImage imageNamed:filename]

This method returns null only on the device.

I know it's known problem and it's usually due to the fact that simulator is case insensitive. I have tried solutions proposed here: UIImage imageNamed returns nil

But nothing worked out for me.

The case is simple: I have 4 files named:Bar@2x~ipad.png, Bar@2x~iphone.png, Bar~ipad.png, Bar~iphone.png. All of them are in project with target checkbox checked.

NSLog(@"%@",[UIImage imageNamed:@"Bar"]);

That line of code gives me null for device and I really have no idea what I'm doing wrong right now.

Upvotes: 12

Views: 10251

Answers (8)

Rithesh Rao
Rithesh Rao

Reputation: 1250

I would like to add one more important point to all the above solutions

Make sure you add your image resource to right target

By mistake if Developer mess-up link with resource and target then conditions arise.

if you have multiple target then double check the resource are set to correct target.

Attached screenshot example in case of resource link between multiple targets.

enter image description here

Upvotes: 0

Vicky
Vicky

Reputation: 620

I also have same issue then : XCode - Build Phases - Copy Bundle Resources -{see image is available or not}- add{image} - clean - delete app - Run .

Upvotes: 0

Mike Gledhill
Mike Gledhill

Reputation: 29151

This is an odd problem, which I hadn't seen until this week.

Below are my findings, and a workaround to your problem.

In my iPhone app, I download an image and store it locally, and this had always worked fine.

But now when I run the same code, it was suddenly failing to create a UIImage out of it using the imageNamed function, and now it was returning nil.

Three notes though:

  1. This exact code did work before, using the same source code and .png image files. I'm not sure if my copy of XCode 6.x or iOS 8.x quietly updated itself in the meantime.
  2. The code continues to work okay (with the same image file) on the iPhone simulator. It just doesn't work on a real device.
  3. Take a look at the code below. When UIImage:imageNamed failed, I ran some code to check if the file actually existed.. and it did. Then I loaded the binary data from the file using NSData:contentsAtPath (which also proves that the file exists and was in the right folder), then created a UIImage out of that, and it worked fine.

Huh ?!

UIImage* img = [UIImage imageNamed:backgroundImageFilename];
if (img != nil)
{
    //  The image loaded fine (this always worked before).  Job done.
    //  We'll set our UIImageView "imgBackgroundView" to contain this image.
    self.imgBackgroundView.image = img;
}
else
{
    //  We were unable to load the image file for some reason.
    //  Let's investigate why.

    //  First, I checked whether the image was actually on the device, and this returned TRUE...
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:backgroundImageFilename];
    if (fileExists)
        NSLog(@"Image file does exist.");
    else
        NSLog(@"Image file does not exist.");


    //  Next, I attempted to just load the bytes in the file, and amazingly, this also worked fine...
    NSData *data = [[NSFileManager defaultManager] contentsAtPath:backgroundImageFilename];
    if (data != nil)
    {
        //  ..and then I COULD actually create a UIImage out of it.
        img = [UIImage imageWithData:data];
        if (img != nil)
        {
            //  We have managed to load the .png file, and can now
            //  set our UIImageView "imgBackgroundView" to contain this image.
            self.imgBackgroundView.image = img;
        }
    }
}

As I said, this code does provide a workaround for this problem, but it's very odd that it's suddenly started happening.

And, I should say, I did try the other suggestions in this thread, cleaning the project, removing the DerivedData, completely removing the app from the device, and so on, but they didn't make any difference.

I would be interested in knowing if anyone else hits this issue, and finds that my code sample works for them.

Update

I'm an idiot.

I'm not sure if the UIImage:imageNamed function has changed or something (and if so, why it continues to work okay on the iPhone 8.1 Simulator), but I found the following one line does work okay:

UIImage* img = [[UIImage alloc] initWithContentsOfFile:backgroundImageFilename];

So it seems as though you should use this function for loading images which aren't a part of your app's bundle.

Upvotes: 0

superandrew
superandrew

Reputation: 1771

This just happened to me, and to discover was very tough: I had one image which where nil just on device

logoWhite.png

my code:

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"LogoWhite"] forBarMetrics:UIBarMetricsDefault];

After a while of debugging, I noticed that the name of the image is beginning with capital case letter. Obviously this doesn't matter on OSX with a ignore case file system. iOs file system isn't, so the image worked on the simulator but not on the device.

I bet that all the solutions about cleaning derived data and rebuild did randomly end with renaming the image, and this would do the trick as well. Just posting here for future reference :)

Upvotes: 1

LetBulletFlies
LetBulletFlies

Reputation: 285

I encountered this issue and just fixed it. I listed the solution below as a reference.

I have several images, and use [UIImage imageNamed:filePath] to show them. All of images displayed well except 1 on simulator/device, but all of them can be seen on OS X. For that image, [UIImage imageNamed] always return null.

After few minutes' investigation, I found that the image cause problem is far big in file size: 4.1M. Others are all around 800kb. They have nearly same size in dimension.

Then I try to open it in image editor, then re-save it. After this, the size dropped to 800k. problem solved.

The reasons I guess,

  1. [UIImage imageNamed:filePath] has a max file size limit? (low possibility, but need to check official document)

  2. the image itself has some error. But OS X has better tolerance than iOS. so iOS cannot read and return null. This issue is like OS X can play more types of video files than iOS since it support more codecs.

so if you encounter this issue in the future, take a few seconds look at the file size. Hope help.

Upvotes: 0

Adrian
Adrian

Reputation: 1176

I did have such a problem recently too.

After playing with filenames and paths sometimes it helps when you clean and rebuild your project.

Upvotes: 5

Rikkles
Rikkles

Reputation: 3372

Completely clean your build and redo it:

  1. delete the app from the device
  2. option-clean the whole build directory in Xcode (⌘-Shift-K)
  3. quit xcode
  4. delete ~/Library/Developer/Xcode/DerivedData
  5. restart xcode, build and run

Upvotes: 2

Gabriele Petronella
Gabriele Petronella

Reputation: 108091

I found myself in the same situation recently.

The solution in my case was adding the extension to the file name.

[UIImage imageNamed:@"Bar.png"]

Upvotes: 4

Related Questions