下面是我写的两个页面:
我的页面:
我的页面获取个人资料代码:
- (void)viewDidLoad {
[super viewDidLoad];
//获取数据
[self getUserInfoData];
//监听事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getUserInfoData) name:@"UserInfoChangeSuccess" object:nil];
}
- (void)getUserInfoData {
//由于在对头像视图通过URL添加图片时,第三方SDWebimage会对图片进行缓存,如果不及时的清除,依然会显示上次缓存的图片,
[[SDImageCache sharedImageCache] clearDisk];
NSString *urlString = @"";
NSString *urlStr = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//网络请求
[HttpHelper requestMethod:@"GET" urlStr:urlStr token:value parma:nil success:^(id json) {
//NSUserDefaults将数据存储到本地
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([json[@"data"][@"userInfo"] objectForKey:@"portrait"]) {
NSString *portrait = json[@"data"][@"userInfo"][@"portrait"];
[defaults setObject:portrait forKey:@"portrait"];
//头像视图显示图片,这一步对头像视图添加图片是为了防止本地没有URL时,可通过网络请求获取
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://123.56.77.171/app%@", portrait]];
[_centerView.headImageView sd_setImageWithURL:url placeholderImage:nil options:SDWebImageRefreshCached];
}
[defaults synchronize];
}
} failure:^(NSError *error) {
}];
}
修改个人资料页面:
修改头像并将头像URL存储到本地:
//上传修改后的头像
- (void)uploadHeadImage {
NSString *urlString =@"";
NSString *urlStr = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[HttpHelper uploadFileWithMethod:@"PUT" URL:urlStr token:value name:@"image" fileName:@"image.jpg" param:nil image:_dataView.headPhotoImage.image success:^(id json) {
if (json) {
//修改头像成功之后,依然将URL保存到本地
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *portrait = [NSString stringWithFormat:@"http://123.56.77.171/app%@", json[@"data"]];
[defaults setObject:portrait forKey:@"portrait"];
[defaults synchronize];
//返回到我的页面
[weakself.navigationController popViewControllerAnimated:YES];
//添加通知,头像更新成功之后,我的页面重新获取一次数据
[[NSNotificationCenter defaultCenter] postNotificationName:@"UserInfoChangeSuccess" object:nil];
}
} failure:^(NSError *error) {
} progress:^(long long totalBytesWritten, long long totalBytesExpectedToWrite) {
}];
}