在ios中的Document中创建多级文件夹以及文件
http://yourgame.iteye.com/blog/1313044
-
- (NSString *)dataPath:(NSString *)file
-
{
-
-
NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"badge"];
-
BOOL bo = [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
-
NSAssert(bo,@"创建目录失败");
-
-
NSString *result = [path stringByAppendingPathComponent:file];
-
-
return result;
-
-
}
-
-
-
-
- (void)viewDidLoad
-
{
-
[super viewDidLoad];
-
-
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
-
-
-
NSString *imageDir = [[[paths objectAtIndex:0] stringByAppendingPathComponent:@"163"] stringByAppendingPathComponent:@"songzi"];
-
-
-
NSString *imagePath =[imageDir stringByAppendingPathComponent:@"0.png"];
-
-
NSData *data = nil;
-
-
-
if([self isExistsFile:imagePath]){
-
data=[NSData dataWithContentsOfFile:imagePath];
-
}else{
-
data = [NSData dataWithContentsOfURL:[NSURL URLWithString: @"]];
-
-
-
[[NSFileManager defaultManager] createDirectoryAtPath:imageDir withIntermediateDirectories:YES attributes:nil error:nil];
-
-
-
[UIImagePNGRepresentation([UIImage imageWithData:data]) writeToFile:imagePath atomically:YES];
-
}
-
imageView.image = [UIImage imageWithData:data];
-
}
stringByAppendingPathComponent和stringByAppendingString的区别
http://www.cnblogs.com/ZhangYuGe/p/4425609.html
cyy:自我理解,不保证正确,但亲自试验确实可行:若用 stringByAppendingString 则需要手动在名称前加 “/”符号,而stringByAppendingPathComponent则不需要,它会自动添加
stringByAppendingPathComponent和stringByAppendingString的区别:
NSString *imagePath = [skinPath stringByAppendingString:[NSString stringWithFormat:@"/%@",imageName]];//stringByAppendingString是在skinPath加后缀的意思
NSString *imagePath = [skinPath stringByAppendingPathComponent:imageName];//stringByAppendingPathComponent是在skinPath后面加上“/”号连接imageName让它成为完整的路径
NSLog(@"imagePath:%@",imagePath);
阅读(819) | 评论(0) | 转发(0) |