如何在Xcode中获取PNG名称而不是整个目录路径?

时间:2012-05-19 22:24:05

标签: objective-c xcode png filenames nsbundle

所以我的“支持文件”Xcode项目中有一个PNG列表。我这样做是为了访问扩展名为PNG的所有文件:

NSArray *pngFilePaths = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:@""];
NSLog(@"PNG:%@", pngFilePaths);

打印出来:

"/Users/John/Library/Application Support/iPhone Simulator/5.1/Applications/123456-1234-1234-1234-B123412341234/MyPNGProject.app/UI_daily_time.png"

但是,我只想要PNG NAME“UI_daily_time”,而不是整个路径。所以我刚刚添加了上面的目录以获取png:

NSArray *pngFilePaths = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:@"/Users/John/Library/Application Support/iPhone Simulator/5.1/Applications/123456-1234-1234-1234-B123412341234/MyPNGProject.app/"];
NSLog(@"PNG:%@", pngFilePaths);

但是pngFilePaths现在是空的,我猜测我给它的“inDirectory”路径是错误的,但我不知道为什么。非常感谢任何帮助,提前感谢!

1 个答案:

答案 0 :(得分:3)

获得完整路径后,使用stringByDeletingPathExtension来删除扩展名,然后使用lastPathComponent获取名称:

NSString *pngFilePath = [pingFilePaths objectAtIndex:0];
NSString *pngName = [[pngFilePath stringByDeletingPathExtension] lastPathComponent];