感谢xjfxjf的分享!
包括下载url图片到沙盒中,自己写了一个类,所有的方法都有注释,通过调用就可以实现功能,有不足之处望大家指出。
//
// UpdateManager.h
// ProjectA v2
//
// Created by Joe.Xi on 11-9-20.
// Copyright 2011年 __MyCompanyName__. All rights reserved.
//
#import
typedef enum {
item = 0 ,
image = 1 ,
} sandboxItemtype;
@interface UpdateManager : NSObject {
sandboxItemtype _itemType;//存储信息类型,暂时没用上
NSString *_filePath;//记录更新日志存储路径
}
- (id)initwithType:(sandboxItemtype)Type;//初始化
- (NSString *)getObjectbykey:(NSString *)key;//根据关键字获取值
- (UIImage *)getImagefrompath:(NSString *)path;//根据路径获取图像
- (NSData *)transformDictodata:(NSDictionary *)dic;//json解析将字典转化成数据
- (NSDictionary *)transformDatatodic:(NSData *)reader;//json解析将数据转化成字典
- (void)cleanText;//清空更新日志
- (void)removeObjectbykey:(NSString *)key;//根据关键字删除记录
- (void)addObjectbykey:(NSString *)key value:(NSString *)value;//添加记录
- (void)updateValuebykey:(NSString *)key value:(NSString *)value;//更新记录
- (void)savePicturefromurl:(NSString *)url path:(NSString *)path imgName:(NSString *)imgName;//根据url将图片下载并保至沙盒,同时记录在更新日志中
- (void)savePicturefromimage:(UIImage *)image path:(NSString *)path imgName:(NSString *)imgName;//
@end

剩余部分
//
// UpdateManager.m
// ProjectA v2
//
// Created by Joe.Xi on 11-9-20.
// Copyright 2011年 __MyCompanyName__. All rights reserved.
//
#import "UpdateManager.h"
#import "CJSONDeserializer.h"
#import "CJSONSerializer.h"
@implementation UpdateManager
- (id)initwithType:(sandboxItemtype)Type
{
self = [super init];
if (self) {
NSString* documentsDirectory= [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
_filePath= [[NSString alloc]initWithString:[documentsDirectory stringByAppendingPathComponent:@"test.txt"]];
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)cleanText
{
NSMutableData *writer = [[NSMutableData alloc]init];
[writer appendData:[@"" dataUsingEncoding:NSUTF8StringEncoding]];
[writer writeToFile:_filePath atomically:NO];
[writer release];
}
#pragma mark - JsonAnalyze
- (NSDictionary *)transformDatatodic:(NSData *)reader
{
NSObject *obj=[[CJSONDeserializer deserializer] deserialize:reader error:nil];
if([obj isKindOfClass:[NSDictionary class]])
{
NSDictionary* dic = (NSDictionary *)obj;
return dic;
}
return nil;
}
- (NSData *)transformDictodata:(NSDictionary *)dic
{
NSObject *obj=[[CJSONSerializer serializer]serializeObject:dic error:nil];
if([obj isKindOfClass:[NSData class]])
{
NSData* data = (NSData *)obj;
return data;
}
return nil;
}
#pragma mark - FilePart
- (NSString *)getObjectbykey:(NSString *)key
{
NSData *reader = [NSData dataWithContentsOfFile:_filePath];
return [[self transformDatatodic:reader] objectForKey:key];
}
- (void)addObjectbykey:(NSString *)key value:(NSString *)value
{
NSData *reader = [NSData dataWithContentsOfFile:_filePath];
NSDictionary *dic=[[NSMutableDictionary dictionary] retain];
dic=[[NSMutableDictionary alloc]initWithDictionary:[self transformDatatodic:reader]];
[dic setValue:value forKey:key];
NSData *write=[self transformDictodata:(NSDictionary *)dic];
[write writeToFile:_filePath atomically:NO];
}
- (void)updateValuebykey:(NSString *)key value:(NSString *)value
{
NSData *reader = [NSData dataWithContentsOfFile:_filePath];
NSDictionary *dic=[[NSMutableDictionary dictionary] retain];
dic=[[NSMutableDictionary alloc]initWithDictionary:[self transformDatatodic:reader]];
[dic setValue:value forKey:key];
NSData *write=[self transformDictodata:(NSDictionary *)dic];
[write writeToFile:_filePath atomically:NO];
}
- (void)removeObjectbykey:(NSString *)key
{
NSData *reader = [NSData dataWithContentsOfFile:_filePath];
NSMutableDictionary *dic=[[NSMutableDictionary dictionary] retain];
dic=[[NSMutableDictionary alloc]initWithDictionary:[self transformDatatodic:reader]];
[dic removeObjectForKey:key];
}
#pragma mark - PicturePart
- (void)savePicturefromurl:(NSString *)url path:(NSString *)path imgName:(NSString *)imgName
{
UIImage *img=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
[self savePicturefromimage:img path:path imgName:imgName];
}
- (void)savePicturefromimage:(UIImage *)image path:(NSString *)path imgName:(NSString *)imgName
{
if(path==nil)
{
path = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:imgName];
}
[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
[self addObjectbykey:imgName value:path];
}
-(UIImage *)getImagefrompath:(NSString *)path
{
return [[UIImage alloc]initWithContentsOfFile:path];
}
@end

[dl href=”http://vdisk.weibo.com/s/9xcWR”]点此下载本地文件的读写源码[/dl]