2

code:

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

It wil retriever the path like this

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

From the above path i need to retrieve only "original/1-1.jpg". please help me.....

Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
iAppDeveloper
  • 1,088
  • 1
  • 8
  • 16
  • Refer [this](http://stackoverflow.com/questions/8079711/getting-the-last-2-directories-of-a-file-path) and [this](http://stackoverflow.com/a/9107645/1126111) – Hector Nov 05 '12 at 04:58

3 Answers3

1

You can use NSString member function lastPathComponent.

NSString filename = [yourString lastPathComponent];

EDIT : Sorry It seems you are not looking for filename from the path, but for a subpath starting from parent folder of filename. Above method only gives you filename string. But you can do this way..

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

and you can use it like this

NSString *subPath  = [self getSubPath:origPath];
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
1

There are several ways to skin a cat:

NSString* lastFile = [path lastPathComponent];
NSString* lastDir = [[path stringByDeletingLastPathComponent] lastPathComponent];
NSString* fullLast = [lastDir stringByAppendingPathComponent:lastFile];
yfrancis
  • 2,616
  • 1
  • 17
  • 26
1

The following code can be used if the resource is in an arbitrary subdirectory of the application bundle. It does not make the assumption that the resource is exactly one subdirectory below the bundle path.

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

For example

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

will result in

relativePath = sub/dir/image.jpg
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382