Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImage not being loaded but file is there

I can't seem to load an image, but I'm sure it is at the path I specify.

Here is the code that tries to load the image:

- (UIImage*) CPPlistImageToUIImage: (NSString *) CPPlistImage {
    /*TODO: Is this a memory leak? Find out*/
    UIImage * ret = [UIImage imageNamed: CPPlistImage];
    if(ret == nil){
        RDLogString(@"Warning: Failed to load image: \"%@\"", CPPlistImage);
    }

    return ret;
}

And here is a function that lists all the files in the directory where this function looks:

- (void) lsOwnDirectory {
    NSError * error = [[NSError alloc] init];
    NSArray * files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: pathToOwnDirectory error: &error];

    for(NSString * file in files){
        RDLogString(@"%@", file);
    }
}

And here is a sample of the output of both functions in the same run:

// CPPlist...Image telling he can't find/load the image
2009-11-29 02:12:51.510 CPPlayer[6452:207] CPConversation.m:113 Warning: Failed to load image: "/Users/nick/Library/Application Support/iPhone Simulator/User/Applications/D3403591-3A56-4F05-B4C1-C21DAAF918A2/CPPlayer.app/CP.Resources/CP.Conversations/Supermarkt/OIGNONS.png"
...
// lsOwnDirectory is called AFTER CPPList...Image
2009-11-29 02:12:51.557 CPPlayer[6452:207] RootViewController.m:81 Contents of /Users/nick/Library/Application Support/iPhone Simulator/User/Applications/D3403591-3A56-4F05-B4C1-C21DAAF918A2/CPPlayer.app/CP.Resources/CP.Conversations/Supermarkt
2009-11-29 02:12:51.566 CPPlayer[6452:207] CPConversation.m:133 audio
2009-11-29 02:12:51.567 CPPlayer[6452:207] CPConversation.m:133 CPConversation.plist
2009-11-29 02:12:51.568 CPPlayer[6452:207] CPConversation.m:133 CPData.sqlite
2009-11-29 02:12:51.569 CPPlayer[6452:207] CPConversation.m:133 images
2009-11-29 02:12:51.570 CPPlayer[6452:207] CPConversation.m:133 OIGNONS.png

I have no clue why it can't find the file. One function sees it, the other does not, they seem to look in the same directory...

Any help, greatly appreciated,

Thanks in advance, Nick

Edit: I forgot to add that the code is executed and compiled by xcode for the iPhone OS 3.0. You probably got that from the tags but still.

like image 620
Nick Avatar asked Dec 01 '22 11:12

Nick


1 Answers

-[UIImage imageNamed:] does not accept a full path to the image file - it only accepts a filename and then looks for an image with that filename in the application's main bundle. That's why it returns nil. -[UIImage imageWithContentsOfFile:], on the other hand, expects a full or partial path to the file. The major difference between the two constructors is that imageNamed: caches the image internally (in a private cache specific to UIImage), whereas imageWithContentsOfFile: will not cache the image.

Sidenote: No, that's not a memory leak. You're using a convenience constructor which according to the memory management rules for Cocoa should return an autoreleased object that you do not take ownership of unless you explicitly retain it elsewhere.

like image 95
Bryan Henry Avatar answered Dec 04 '22 22:12

Bryan Henry