图片备份记录(头像使用记录)键值存在dic,序列化到data,存储在file中

 
  
 1 #import <Foundation/Foundation.h>
2
3 typedef enum {
4 item = 0 ,
5 image = 1 ,
6 avatar = 2
7 } sandboxItemtype;
8
9
10 @interface UpdateManager : NSObject {
11 sandboxItemtype _itemType;//存储信息类型,暂时没用上
12 NSString *_filePath;//记录更新日志存储路径
13 NSString *_timePath;
14 NSString *_avatarPath;//记录用户曾经使用过的头像队列
15 }
16 - (id)initwithType:(sandboxItemtype)Type;//初始化
17 - (UIImage *)getCurrentavatar;//获取用户当前头像
18 - (NSArray *)getAvatarary;//获取用户历史头像队列
19
20 - (NSString *)getTimebykey:(NSString *)key;
21 - (NSString *)getObjectbykey:(NSString *)key;//根据关键字获取值,对象是图像其保存的值就是图片在沙盒中的路径,再通过路径获取图片
22 - (UIImage *)getImagefrompath:(NSString *)path;//根据路径获取图像
23 - (NSData *)transformDictodata:(NSDictionary *)dic;//json解析将字典转化成数据
24 - (NSDictionary *)transformDatatodic:(NSData *)reader;//json解析将数据转化成字典
25
26 - (void)cleanText;//清空更新日志
27 - (void)cleanAvatarary;//清空用户历史头像队列
28 - (void)setAvatar:(NSString *)key;//添加头像
29 - (void)avatarChoosed:(NSString *)path;//被选中头像
30 - (BOOL)isValid:(NSString *)timestamp;//判断头像是否过期,沙盒只保留1个月内用户使用过的头像
31 - (void)recordTimebykey:(NSString *)key;//如果其他属性也需要记录时间时使用
32 - (void)removeObjectbykey:(NSString *)key;//根据关键字删除记录
33 - (void)removeImagebypath:(NSString *)path;//根据路径删除图片
34 - (BOOL)notCurrent:(NSString *)key keyAry:(NSArray *)keyAry;//判断头像是否为用户当前头像
35 - (void)addObjectbykey:(NSString *)key value:(NSString *)value;//添加记录
36 - (void)updateValuebykey:(NSString *)key value:(NSString *)value;//更新记录
37 - (void)savePicturefromurl:(NSString *)url path:(NSString *)path imgName:(NSString *)imgName;//根据url将图片下载并保至沙盒,同时记录在更新日志中
38 - (void)savePicturefromimage:(UIImage *)image path:(NSString *)path imgName:(NSString *)imgName;//
39 @end

 1 #import "UpdateManager.h"
2 #import "CJSONDeserializer.h"
3 #import "CJSONSerializer.h"
4
5 @implementation UpdateManager
6
7 - (id)initwithType:(sandboxItemtype)Type
8 {
9 self = [super init];
10 if (self) {
11 _itemType=Type;
12 NSString* documentsDirectory= [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
13 _filePath= [[NSString alloc]initWithString:[documentsDirectory stringByAppendingPathComponent:@"test.txt"]];
14 _timePath= [[NSString alloc]initWithString:[documentsDirectory stringByAppendingPathComponent:@"time.txt"]];
15 _avatarPath= [[NSString alloc]initWithString:[documentsDirectory stringByAppendingPathComponent:@"avatar.txt"]];
16 }
17 return self;
18 }
19 - (void)dealloc
20 {
21 [super dealloc];
22 }
23 - (void)cleanText
24 {
25 NSMutableData *writer = [[NSMutableData alloc]init];
26 [writer appendData:[@"" dataUsingEncoding:NSUTF8StringEncoding]];
27 [writer writeToFile:_filePath atomically:NO];
28 [writer release];
29 }
30 #pragma mark - JsonAnalyze
31 - (NSDictionary *)transformDatatodic:(NSData *)reader
32 {
33 NSObject *obj=[[CJSONDeserializer deserializer] deserialize:reader error:nil];
34 if([obj isKindOfClass:[NSDictionary class]])
35 {
36 NSDictionary* dic = (NSDictionary *)obj;
37 return dic;
38 }
39 return nil;
40 }
41 - (NSData *)transformDictodata:(NSDictionary *)dic
42 {
43 NSObject *obj=[[CJSONSerializer serializer]serializeObject:dic error:nil];
44 if([obj isKindOfClass:[NSData class]])
45 {
46 NSData* data = (NSData *)obj;
47 return data;
48 }
49 return nil;
50 }
51 #pragma mark - FilePart
52 //对更新日志文件进行添加修改查找或者删除,包括对沙盒中图片的删除
53 - (NSString *)getObjectbykey:(NSString *)key
54 {
55 NSData *reader = [NSData dataWithContentsOfFile:_filePath];
56 return [[self transformDatatodic:reader] objectForKey:key];
57 }
58 - (void)addObjectbykey:(NSString *)key value:(NSString *)value
59 {
60 NSData *reader = [NSData dataWithContentsOfFile:_filePath];
61 NSDictionary *dic=[[NSMutableDictionary dictionary] retain];
62 dic=[[NSMutableDictionary alloc]initWithDictionary:[self transformDatatodic:reader]];
63 [dic setValue:value forKey:key];
64 NSData *write=[self transformDictodata:(NSDictionary *)dic];
65 [write writeToFile:_filePath atomically:NO];
66 }
67 - (void)updateValuebykey:(NSString *)key value:(NSString *)value
68 {
69 NSData *reader = [NSData dataWithContentsOfFile:_filePath];
70 NSDictionary *dic=[[NSMutableDictionary dictionary] retain];
71 dic=[[NSMutableDictionary alloc]initWithDictionary:[self transformDatatodic:reader]];
72 [dic setValue:value forKey:key];
73 NSData *write=[self transformDictodata:(NSDictionary *)dic];
74 [write writeToFile:_filePath atomically:NO];
75 }
76 - (void)removeImagebypath:(NSString *)path
77 {
78 NSFileManager* fileManager = [NSFileManager defaultManager];
79 if (path && [fileManager fileExistsAtPath:path]) {
80 [fileManager removeItemAtPath:path error:nil];
81 }
82 }
83 - (void)removeObjectbykey:(NSString *)key
84 {
85 NSData *reader = [NSData dataWithContentsOfFile:_filePath];
86 NSMutableDictionary *dic=[[NSMutableDictionary dictionary] retain];
87 dic=[[NSMutableDictionary alloc]initWithDictionary:[self transformDatatodic:reader]];
88 [self removeImagebypath:[dic objectForKey:key]];
89 [dic removeObjectForKey:key];
90 NSData *write=[self transformDictodata:(NSDictionary *)dic];
91 [write writeToFile:_filePath atomically:NO];
92 }
93 #pragma mark - PicturePart
94 //保存本地或url图片至app沙盒
95 - (void)savePicturefromurl:(NSString *)url path:(NSString *)path imgName:(NSString *)imgName
96 {
97 UIImage *img=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
98 [self savePicturefromimage:img path:path imgName:imgName];
99 }
100
101 - (void)savePicturefromimage:(UIImage *)image path:(NSString *)path imgName:(NSString *)imgName
102 {
103 if(path==nil)
104 {
105 path = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:imgName];
106 }
107 [UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
108 [self addObjectbykey:imgName value:path];
109 }
110 - (UIImage *)getImagefrompath:(NSString *)path
111 {
112 return [[UIImage alloc]initWithContentsOfFile:path];
113 }
114 #pragma mark - Avatar
115 //保存用户使用过的头像至沙盒,并记录,在_filePath更新日志文件中通过CurrentAvatar关键字可访问用户当前头像,_avatarPath保存用户历史头像
116 - (void)setAvatar:(NSString *)key
117 {
118 NSData *reader = [NSData dataWithContentsOfFile:_filePath];
119 NSDictionary *dic=[[NSMutableDictionary dictionary] retain];
120 dic=[[NSMutableDictionary alloc]initWithDictionary:[self transformDatatodic:reader]];
121 [dic setValue:[self getObjectbykey:key] forKey:@"CurrentAvatar"];
122 NSData *write=[self transformDictodata:(NSDictionary *)dic];
123 [write writeToFile:_filePath atomically:YES];
124 [dic release];
125
126 reader=[NSData dataWithContentsOfFile:_avatarPath];
127 dic=[[NSMutableDictionary dictionary] retain];
128 dic=[[NSMutableDictionary alloc]initWithDictionary:[self transformDatatodic:reader]];
129 [dic setValue:[self getObjectbykey:key] forKey:[NSString stringWithFormat:@"%d",time(NULL)]];
130 write=[self transformDictodata:(NSDictionary *)dic];
131 [write writeToFile:_avatarPath atomically:NO];
132 }
133 - (UIImage *)getCurrentavatar
134 {
135 return [self getImagefrompath:[self getObjectbykey:@"CurrentAvatar"]];
136 }
137 - (void)cleanAvatarary
138 {
139 NSData *reader = [NSData dataWithContentsOfFile:_avatarPath];
140 NSMutableDictionary *dic=[[NSMutableDictionary dictionary] retain];
141 dic=[[NSMutableDictionary alloc]initWithDictionary:[self transformDatatodic:reader]];
142 [dic removeAllObjects];
143 NSData *write=[self transformDictodata:(NSDictionary *)dic];
144 [write writeToFile:_avatarPath atomically:NO];
145 }
146 - (NSArray *)getAvatarary
147 {
148 NSData *reader = [NSData dataWithContentsOfFile:_avatarPath];
149 NSMutableDictionary *dic=[[NSMutableDictionary dictionary] retain];
150 dic=[[NSMutableDictionary alloc]initWithDictionary:[self transformDatatodic:reader]];
151 NSArray *keyAry=[dic allKeys];
152 for (int i=0; i<[keyAry count]; i++) {
153 if(![self isValid:[keyAry objectAtIndex:i]]&&[self notCurrent:[keyAry objectAtIndex:i] keyAry:keyAry])//排除掉当前头像(当前头像的时间蔟最大)
154 {
155 [self removeImagebypath:[dic objectForKey:[keyAry objectAtIndex:i]]];
156 [dic removeObjectForKey:[keyAry objectAtIndex:i]];
157 }
158 }//删除掉非当前、1个月前的头像
159 NSData *write=[self transformDictodata:(NSDictionary *)dic];
160 [write writeToFile:_avatarPath atomically:NO];
161 NSArray *pathAry=[dic allValues];
162 [dic release];
163 return pathAry;
164 }
165 - (void)avatarChoosed:(NSString *)path
166 {
167 NSData *reader = [NSData dataWithContentsOfFile:_avatarPath];
168 NSMutableDictionary *dic=[[NSMutableDictionary dictionary] retain];
169 dic=[[NSMutableDictionary alloc]initWithDictionary:[self transformDatatodic:reader]];
170 NSArray *valueAry=[dic allKeys];
171 for (int i=0;i<[dic count]; i++) {
172 if ([[dic objectForKey:[valueAry objectAtIndex:i]] isEqualToString:path]) {
173 [dic setValue:path forKey:[NSString stringWithFormat:@"%d",time(NULL)]];
174 [dic removeObjectForKey:[valueAry objectAtIndex:i]];
175 break;
176 }
177 }
178 NSData *write=[self transformDictodata:(NSDictionary *)dic];
179 [write writeToFile:_avatarPath atomically:YES];
180 [dic release];
181
182 reader = [NSData dataWithContentsOfFile:_filePath];
183 dic=[[NSMutableDictionary dictionary] retain];
184 dic=[[NSMutableDictionary alloc]initWithDictionary:[self transformDatatodic:reader]];
185 [dic setValue:path forKey:@"CurrentAvatar"];
186 write=[self transformDictodata:(NSDictionary *)dic];
187 [write writeToFile:_filePath atomically:NO];
188 [dic release];
189 }
190 - (BOOL)notCurrent:(NSString *)key keyAry:(NSArray *)keyAry
191 {
192 for(int i=0;i<[keyAry count];i++)
193 {
194 if([key intValue]<[[keyAry objectAtIndex:i]intValue])
195 {
196 return YES;
197 }
198 }
199 return NO;
200 }
201 - (BOOL)isValid:(NSString *)timestamp
202 {
203 if((time(NULL)-[timestamp intValue])>60*60*24*30)
204 {
205 return NO;
206 }
207 return YES;
208 }
209 #pragma mark - TimeStamp
210 //时间戳,用于记录更新时间等信息
211 - (void)recordTimebykey:(NSString *)key
212 {
213 NSData *reader = [NSData dataWithContentsOfFile:_timePath];
214 NSDictionary *dic=[[NSMutableDictionary dictionary] retain];
215 dic=[[NSMutableDictionary alloc]initWithDictionary:[self transformDatatodic:reader]];
216 [dic setValue:[NSString stringWithFormat:@"%d", time(NULL)] forKey:key];
217 NSData *write=[self transformDictodata:(NSDictionary *)dic];
218 [write writeToFile:_timePath atomically:NO];
219 }
220 - (NSString *)getTimebykey:(NSString *)key
221 {
222 NSData *reader = [NSData dataWithContentsOfFile:_timePath];
223 return [[self transformDatatodic:reader] objectForKey:key];
224 }
225 @end      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值