Saturday, May 18, 2013

NSString isAbsolutePath example ios


[NSString isAbsolutePath]

Returning a Boolean value that indicates whether the receiver represents an absolute path.
- (BOOL)isAbsolutePath
Return Value
YES if the receiver (if interpreted as a path) represents an absolute path, otherwiseNO (if the receiver represents a relative path).
Discussion of [NSString isAbsolutePath]
See String Programming Guide for more information on paths.
Note that this method only works with file paths (not, for example, string representations of URLs). The method does not check the filesystem for the existence of the path (use fileExistsAtPath: or similar methods inNSFileManager for that task).
Example of [NSString isAbsolutePath]
-(BOOL) fileExistsAtPath:(NSString*)relPath withSuffix:(NSString*)suffix
{
    NSString *fullpath = nil;

    // only if it is not an absolute path
    if( ! [relPath isAbsolutePath] ) {
        // pathForResource also searches in .lproj directories. issue #1230
        NSString *file = [relPath lastPathComponent];
        NSString *imageDirectory = [relPath stringByDeletingLastPathComponent];

        fullpath = [bundle_ pathForResource:file
                                                   ofType:nil
                                              inDirectory:imageDirectory];

    }

    if (fullpath == nil)
        fullpath = relPath;

    NSString *path = [self getPath:fullpath forSuffix:suffix];

    return ( path != nil );
}
Example of [NSString isAbsolutePath]
// Create an NSString from our filename
 path = [NSString stringWithCString:filename encoding:NSASCIIStringEncoding];
 if( ![path isAbsolutePath] )
 {
  path = [[NSBundle mainBundle] pathForResource:path ofType:@\"png\"];
 }
 
 if( path == nil )
 {
  return false;
 }
Example of [NSString isAbsolutePath]
+ (NSString *)absolutePathToFile:(NSString *)path withScaleFactor:(float)factor {
    NSLog(@"%s", __FUNCTION__);
    NSString *absolutePath = [path isAbsolutePath] ? path : [[NSBundle appBundle] pathForResource:path];
    NSlog(@"factor:%f", factor);
    NSLog(@"[self isDevicePad]:%i", [self isDevicePad]);
    NSLog(@"[SPStage supportPadResolutions]:%i", [SPStage supportPadResolutions]);
 
    if (factor != 1.0f) {
        NSString *suffix = [NSString stringWithFormat:@"@%@x", [NSNumber numberWithFloat:factor]];
        NSString *pathWithScale = [absolutePath stringByAppendingSuffixToFilename:suffix];
        if ([SPUtils fileExistsAtPath:pathWithScale]) return pathWithScale;
    } else if ([self isDevicePad] && [SPStage supportPadResolutions]) {
        NSString *pathWithPadResolution = [absolutePath stringByAppendingSuffixToFilename:sPadResolutionsSuffix];
        NSLog(@"pathWithPadResolution:%@", pathWithPadResolution);
        NSLog(@"[SPUtils fileExistsAtPath:pathWithPadResolution]:%i", [SPUtils fileExistsAtPath:pathWithPadResolution]);
        if ([SPUtils fileExistsAtPath:pathWithPadResolution]) return pathWithPadResolution;
    }
 
    NSLog(@"[SPUtils fileExistsAtPath:absolutePath]:%i", [SPUtils fileExistsAtPath:absolutePath]);
    if ([SPUtils fileExistsAtPath:absolutePath])
        return absolutePath;
    else
        return nil;
}