标签:
//取得Documents路径的方法:
- (NSString *)documentFolder {
return [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
}
//取得Documents中某个文件的路径
NSString *path = [[self documentFolder] stringByAppendingPathComponent:@"image.png"];
//获取tmp目录
NSString *tempPath = NSTemporaryDirectory();
//补充:取得应用程序包(即bundle)的路径
- (NSString *)bundleFolder {
return [[NSBundle mainBundle]bundlePath];
}
//1,获取家目录路径的函数:
NSString *homeDir = NSHomeDirectory();
//2,获取Documents目录路径的方法:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *docDir = [paths objectAtIndex:0];
//3,获取Caches目录路径的方法:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);
NSString *cachesDir = [paths objectAtIndex:0];
//4,获取tmp目录路径的方法:
NSString *tmpDir = NSTemporaryDirectory();
//5,获取应用程序程序包中资源文件路径的方法:
//例如获取程序包中一个图片资源(apple.png)路径的方法:
NSString *imagePath = [[NSBundle mainBundle]pathForResource:@"apple"ofType:@"png"];
UIImage *appleImage = [[UIImage alloc]initWithContentsOfFile:imagePath];
//代码中的mainBundle类方法用于返回一个代表应用程序包的对象。
- (BOOL)writeApplicationData:(NSData*)data toFile:(NSString*)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *docDir = [paths objectAtIndex:0];
if(!docDir) {
NSLog(@"Documents directory not found!");
return NO;
}
NSString *filePath = [docDir stringByAppendingPathComponent:fileName];
return [data writeToFile:filePath atomically:YES];
}
- (NSData *)applicationDataFromFile:(NSString *)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *filePath = [docDir stringByAppendingPathComponent:fileName];
NSData *data = [[NSData alloc]initWithContentsOfFile:filePath];
return data;
}
标签:
原文地址:http://www.cnblogs.com/huangzs/p/4489697.html