2012-11-05 2 views
2

코드 : 그것은 내가 단지 "원래/1-1.jpg"를 검색해야 위의 경로에서이폴더 이름 및 이미지 이름을 가져옵니다

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

같은 경로를 리트리버 줘야

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

. 도와주세요 .....

+0

에 http://stackoverflow.com/questions/8079711/getting-the-last-2- [이 (참조를 초래할 것 디렉토리 경로 - 파일 경로) 및 [this] (http://stackoverflow.com/a/9107645/1126111) – Hector

답변

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 
+0

고맙습니다. –

1

NSString 멤버 함수 lastPathComponent를 사용할 수 있습니다.

NSString filename = [yourString lastPathComponent]; 

EDIT : 죄송합니다. 위의 메서드는 파일 이름 문자열 만 제공합니다. 하지만 당신은

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

고양이 피부 방법에는 여러 가지가 있습니다 :

NSString* lastFile = [path lastPathComponent]; 
NSString* lastDir = [[path stringByDeletingLastPathComponent] lastPathComponent]; 
NSString* fullLast = [lastDir stringByAppendingPathComponent:lastFile]; 
관련 문제