為了實(shí)現(xiàn)用戶登錄 再輸入賬號(hào)時(shí)候加載已經(jīng)登錄過的用戶頭像
先說思路祸憋,然后根據(jù)思路上代碼
1会宪、用戶登錄后獲取用戶信息并儲(chǔ)存
?2、當(dāng)用戶更換頭像后需要替換之前的
3蚯窥、登錄時(shí)根據(jù)用戶輸入的賬號(hào)去加載本地儲(chǔ)存的相對(duì)應(yīng)的用戶頭像
---------------------------------------代碼---------------------------------------
宏定義
#define JTUserDefaults [NSUserDefaults standardUserDefaults]?
1掸鹅、儲(chǔ)存信息:創(chuàng)建一個(gè)可變字典將NSUserDefaults對(duì)象存到這個(gè)字典中 然后再講手機(jī)號(hào)作為key值,頭像地址作為value放在這個(gè)字典中拦赠,最后把字典寫入本地
NSMutableDictionary*mutableDic =[NSMutableDictionary dictionaryWithDictionary:[JTUserDefaults objectForKey:@"userImageUrl"]];
[mutableDic setValue:dic[@"avatar_img"] forKey:dic[@"phone"]];
[JTUserDefaults setObject:mutableDic forKey:@"userImageUrl"];
[JTUserDefaults synchronize];
(還可以聲明一個(gè)字典存用戶手機(jī)號(hào)和頭像巍沙,是為了用戶退出后,再去登陸的時(shí)候去讀取加載上次登錄的用戶頭像荷鼠,這個(gè)字典不需要儲(chǔ)存多條)
2句携、更換頭像:取出本地儲(chǔ)存頭像 根據(jù)key(電話號(hào)碼)改變Value(原來的頭像地址)
NSMutableDictionary*mutableDic = [NSMutableDictionary dictionaryWithDictionary:[JTUserDefaults objectForKey:@"userImageUrl"]];
//取出當(dāng)前手機(jī)號(hào),根據(jù)取出的key像替換掉原來的value 也就是替換掉頭像地址
NSString*phone = [NSString stringWithFormat:@"%@",[YCUserData sharedManager].phone];
mutableDic[phone] = obj[@"data"];
[JTUserDefaults setObject:mutableDic forKey:@"userImageUrl"];
[JTUserDefaults synchronize];
3允乐、匹配本地頭像:在這個(gè)textField代理事件中找到對(duì)應(yīng)的頭像
#pragma mark -- textField代理事件
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string {
NSString* toBeString = [textField.textstringByReplacingCharactersInRange:rangewithString:string];
JTDLog(@"%@",toBeString);
if(textField.tag==10) {
if(textField.text.length>=13&& ![stringisEqualToString:@""]) {
returnNO;
}
NSMutableDictionary*mutableDic = [NSMutableDictionarydictionaryWithDictionary:[JTUserDefaultsobjectForKey:@"userImageUrl"]];
if(IsArrOrDicEmpty(mutableDic)) {
}else{
NSArray*phoneArr =mutableDic.allKeys;
if(toBeString.length==0) {
[_headerImagesetImage:IMAGE_NAME(@"user_head")];
returnYES;
}
for(NSString*strinphoneArr) {
if([strcontainsString:toBeString]) {
NSString*imageUrl =[mutableDicobjectForKey:str];
[_headerImagesd_setImageWithURL:[NSURLURLWithString:imageUrl]placeholderImage:IMAGE_NAME(@"user_head")];
break;
}else{
[_headerImagesd_setImageWithURL:[NSURLURLWithString:@""]placeholderImage:IMAGE_NAME(@"user_head")];
}
}
}
}else{
if(textField.text.length>=21&& ![stringisEqualToString:@""]) {
returnNO;
}
}
returnYES;
}