获取文件夹名称和图像名称

时间:2012-11-05 04:38:18

标签: ios objective-c cocoa

代码:

NSString *path = [[NSBundle mainBundle] pathForResource:[objDynaMoThumbnailImages objectAtIndex:eg] ofType:@"jpg" inDirectory:[objDynaMoProductImagePath objectAtIndex:eg]];

它将像这样检索路径

/Users/software/Library/Application Support/iPhone Simulator/6.0/Applications/61AE2605-CE8E-404D-9914-CDA9EBA8027C/DynaMO.app/original/1-1.jpg

从上面的路径我只需要检索“original / 1-1.jpg”。 请帮帮我......

3 个答案:

答案 0 :(得分:2)

有几种方法可以给猫皮肤涂抹:

NSString* lastFile = [path lastPathComponent];
NSString* lastDir = [[path stringByDeletingLastPathComponent] lastPathComponent];
NSString* fullLast = [lastDir stringByAppendingPathComponent:lastFile];

答案 1 :(得分:1)

您可以使用NSString成员函数lastPathComponent。

NSString filename = [yourString lastPathComponent];

编辑:抱歉,您似乎不是从路径中查找文件名,而是从文件名的父文件夹开始的子路径。上面的方法只给你文件名字符串。但你可以这样做..

-(NSString *) getSubPath:(NSString*)origPath{
   NSArray * array = [origPath componentsSeparatedByString:@"/"];
   if(!array  || [array length] == 0 || [array length] == 1)
     return origPath;
   int length  =  [array length];
   return [NSString stringWithFormat:@"%@/%@", [array objectAtIndex:(length - 2)], [array lastObject]];
}

你可以像这样使用它

NSString *subPath  = [self getSubPath:origPath];

答案 2 :(得分:1)

如果资源位于应用程序包的任意子目录中,则可以使用以下代码。它没有假设资源恰好是包路径下面的一个子目录。

NSString *path = [[NSBundle mainBundle] pathForResource:...];
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSString *relativePath;
if ([path hasPrefix:bundlePath]) {
    relativePath = [path substringFromIndex:([bundlePath length] + 1)];
} else {
    relativePath = path;
}

例如

path = /Users/software/Library/Application Support/.../DynaMO.app/sub/dir/image.jpg

将导致

relativePath = sub/dir/image.jpg
相关问题