NSFileManager(文件管理类)
文件管理类, 对文件进行管理
NSFileManager *fm = [NSFileManager defaultManager];
NSString *filePath = [NSString stringWithFormat:@"%@/image", NSHomeDirectory()];
NSLog(@"%@", filePath);
//创建文件夹
NSError *error = nil;
BOOL result = [fm createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:&error];
if (result) {
NSLog(@"创建成功");
} else {
NSLog(@"创建失败: %@", error);
}
移动文件夹
NSString *documentpath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
NSString *filePath1 = [NSString stringWithFormat:@"%@/image", documentpath];
BOOL result =[fm moveItemAtPath:filePath toPath:filePath1 error:&error];
if (result) {
NSLog(@"创建成功");
} else {
NSLog(@"创建失败: %@", error);
}
NSString *imagePath = [[NSBundle mainBundle] bundlePath];
NSString *imagePath1 = [filePath1 stringByAppendingPathComponent:@"1.jpg"];
BOOL result =[fm copyItemAtPath:imagePath toPath:imagePath1 error:&error];
if (result) {
NSLog(@"创建成功");
} else {
NSLog(@"创建失败: %@", error);
}
保存图片
NSURL *url = [NSURL URLWithString:@"http://img3.douban.com/view/event_poster/median/public/4cdefe4f77918b1.jpg"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSString *filePath = [NSString stringWithFormat:@"%@/1.jpg", NSHomeDirectory()];
NSLog(@"%@", filePath);
BOOL result = [data writeToFile:filePath atomically:YES];
if (result) {
NSLog(@"写入成功");
} else {
NSLog(@"写入失败");
}
}];