Search code examples
objective-ciosnsbundle

NSBundle pathForResource not working


On iOS 6 this block of code works fine:

- (NSString*)cLocalImageName
{
     [self willAccessValueForKey:@"cLocalImageName"];
     NSString*pathToImage;
     if ([self.cIsUserCreated boolValue]) {
         pathToImage =  [self primitiveValueForKey:@"cLocalImageName"];
     }else{
         pathToImage = [[NSBundle mainBundle] pathForResource:[self primitiveValueForKey:@"cLocalImageName"] ofType:@".png"];
     }
     [self didAccessValueForKey:@"cLocalImageName"];
     return pathToImage;
}

Namely this line always returns the correct file path:

 pathToImage = [[NSBundle mainBundle] pathForResource:[self primitiveValueForKey:@"cLocalImageName"] ofType:@".png"];

However, on iOS 5 that line always returns nil, and I instead have to call:

 pathToImage = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",[self primitiveValueForKey:@"cLocalImageName"]]];

Can anyone give me any insight into why this is an issue on iOS 5?

Things I've ruled out:

• [self primitiveValueForKey:@"cLocalImageName"] is not returning nil.

• The .png objects are in fact present in the compiled .app package once the app is built.


Solution

  • Shouldn't this line:

    pathToImage = [[NSBundle mainBundle] pathForResource:[self primitiveValueForKey:@"cLocalImageName"] ofType:@".png"];
    

    be this:

    pathToImage = [[NSBundle mainBundle] pathForResource:[self primitiveValueForKey:@"cLocalImageName"] ofType:@"png"];
    

    ?