IOS持久化数据----(保存数据的一系列方法)
来源:互联网 发布:淘宝内容营销怎么做 编辑:程序博客网 时间:2024/04/28 22:05
NSString *astr = @"efg";
NSArray *Array = [NSArray arrayWithObjects:str, astr, nil];
//Save
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if (!documentsDirectory) {
NSLog(@"Documents directory not found!");
}
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"Savedatas.plist"];
[[NSArray arrayWithObjects:Array,nil] writeToFile:appFile atomically:NO];
//load
if([[NSFileManager defaultManager] fileExistsAtPath:appFile])
self.SaveDataArray = [NSMutableArray arrayWithContentsOfFile:appFile];
else
self.SaveDataArray = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Savedatas" ofType:@"plist"]];
NSArray *strArray = [self.SaveDataArray objectAtIndex:0];
str = [strArray objectAtIndex:0];
astr = [strArray objectAtIndex:1];
NSString *astr = @"efg";
NSArray *Array = [NSArray arrayWithObjects:str, astr, nil];
//Save
NSString *Path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *filename = [Path stringByAppendingPathComponent:@"test"];
[NSKeyedArchiver archiveRootObject:Array toFile:filename];
str = @"a";
astr = @"";
//load
NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile: filename];
str = [arr objectAtIndex:0];
astr = [arr objectAtIndex:1];
NSLog(@"str:%@",str);
NSLog(@"astr:%@",astr);
NSString *astr = @"efg";
NSArray *Array = [NSArray arrayWithObjects:str, astr, nil];
//Save
NSUserDefaults *SaveDefaults = [NSUserDefaults standardUserDefaults];
[SaveDefaults setObject:Array forKey:@"SaveKey"];
str = @"a";
astr = @"";
//load
Array = [SaveDefaults objectForKey:@"SaveKey"];
str = [Array objectAtIndex:0];
astr = [Array objectAtIndex:1];
NSLog(@"str:%@",str);
NSLog(@"astr:%@",astr);
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if (!documentsDirectory) {
NSLog(@"Documents directory not found!");
return NO;
}
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
return ([data writeToFile:appFile atomically:YES]);
}
-(id) readApplicationData:(NSString *)fileName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
NSDictionary *myData = [[[NSDictionary alloc] initWithContentsOfFile:appFile] autorelease];
return myData;
}
参数说明:
NSData to retrieve the image from the URL
NSDocumentDirectory to find Document folder’s Path
UIImagePNGRepresentation to save it as PNG
UIImageJPEGRepresentation to save it as JPEG
代码:
NSLog(@”Downloading…”);// Get an image from the URL below
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.objectgraph.com/images/og_logo.png"]]];
NSLog(@”%f,%f”,image.size.width,image.size.height);
// Let’s save the file into Document folder.
// You can also change this to your desktop for testing. (e.g. /Users/kiichi/Desktop/)
// NSString *deskTopDir = @”/Users/kiichi/Desktop”;
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
// If you go to the folder below, you will find those pictures
NSLog(@”%@”,docDir);
NSLog(@”saving png”);
NSString *pngFilePath = [NSString stringWithFormat:@"%@/test.png",docDir];
NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];
[data1 writeToFile:pngFilePath atomically:YES];
NSLog(@”saving jpeg”);
NSString *jpegFilePath = [NSString stringWithFormat:@"%@/test.jpeg",docDir];
NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];//1.0f = 100% quality
[data2 writeToFile:jpegFilePath atomically:YES];
NSLog(@”saving image done”);
[image release];
原文地址:http://hi.baidu.com/841695671/item/a8ba037de5e63a4fee1e532c- IOS持久化数据----(保存数据的一系列方法)
- IOS持久化数据----(保存数据的一系列方法)
- IOS持久化数据----(保存数据的一系列方法)
- 保存数据的一系列方法
- 保存数据的一系列方法
- 保存数据的一系列方法
- 保存数据的一系列方法
- 保存数据的一系列方法
- 数据的持久化保存
- iOS中 数据持久化的方法
- iOS数据持久化方法
- iOS数据持久化方法
- 数据持久化保存
- iOS面试1-iOS数据持久化的方法
- IOS的数据持久化
- android下数据的持久化保存
- 数据持久化的方法
- iOS开发中常见的数据持久化的方法
- C++编程2013.04.10
- 彻底明白Java的IO系统
- 求无序数组第K大的数
- 冒泡排序
- tcp 通信---cilent
- IOS持久化数据----(保存数据的一系列方法)
- hdu 1527 取石子游戏
- ORACLE NVARCHAR与VARCHAR的问题
- android学习笔记---34_Activity的启动模式
- tcp 通信---MultiServer
- 微软设备中心(WMDC)使用教程[手机U盘模式][共享电脑网络]及WP数据
- 怎样在WIN7电脑上调出保护眼睛的颜色&配色...
- tcp 通信---ServerThread
- [转载]Tomcat6配置HTTPS