pathForResource?不使用扩展(Iphone)

lud*_*udo 17 iphone cocoa objective-c

当我使用包中的路径创建图像时,这就是我正在做的事情:


UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image" ofType:@"jpg"]];
Run Code Online (Sandbox Code Playgroud)

我想要做的是尝试找到我的图像的路径,但不使用扩展名,而不使用'ofType'(因为我的图像的名称和她的扩展名存储在我的数据库中)类似的东西:


UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image.jpg"]];
Run Code Online (Sandbox Code Playgroud)

但我不知道该怎么做.

最好的祝福,

Tho*_*ing 41

为什么不拆分从DB获得的字符串?

NSString* fullFileName = @"image.jpg";
NSString* fileName = [[fullFileName lastPathComponent] stringByDeletingPathExtension];
NSString* extension = [fullFileName pathExtension];
Run Code Online (Sandbox Code Playgroud)

现在你可以简单地使用:

[[NSBundle mainBundle] pathForResource:fileName ofType:extension]];
Run Code Online (Sandbox Code Playgroud)

  • 您无需分离扩展名.看我的回答.但是,我不知道使用我的方法是否会对性能产生任何影响. (2认同)

Bro*_*son 24

您可以轻松使用NSBundle方法而不传递扩展名,只需传递nil进行扩展即可.

[[NSBundle mainBundle] pathForResource:@"image.jpg" ofType:nil];
Run Code Online (Sandbox Code Playgroud)

  • 这应该是例外的答案恕我直言,因为它更优雅和重点.我不确定为什么苹果坚持将扩展分开. (2认同)