NSBundle pathForResource无法正常工作

时间:2012-10-30 20:30:24

标签: objective-c ios nsbundle

在iOS 6上,这段代码工作正常:

- (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;
}

即此行始终返回正确的文件路径:

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

然而,在iOS 5上,该行总是返回nil,而我必须调用:

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

任何人都可以告诉我为什么这是iOS 5上的问题吗?

我排除的事情:

•[self primitiveValueForKey:@“cLocalImageName”]未返回nil。

•构建应用程序后,.png对象实际上存在于已编译的.app包中。

1 个答案:

答案 0 :(得分:12)

不应该这一行:

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

是这样的:

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