我得到的圖像作爲從視圖控制器字典URL,我已經通過了字典視圖控制器BI想,如果用戶已經更新了圖像則顯示了更新的圖像否則它顯示了以前圖像,我正在爲它做下面的代碼。仔細檢查並告訴它爲什麼不按需要工作,並且只在每種情況下顯示前一個圖像。更新圖像並將其保存在字典
-(void)showUserImage:(NSURL*)imgUrl
{
[ConnectionManager setSharedCacheForImages];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:imgUrl];
NSURLSession *session = [ConnectionManager prepareSessionForRequest];
NSCachedURLResponse *cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
if (cachedResponse.data) {
UIImage *downloadedImage = [UIImage imageWithData:cachedResponse.data];
dispatch_async(dispatch_get_main_queue(), ^{
_profileImageView.image = downloadedImage;
});
} else {
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;
if(res.statusCode == 200){
dispatch_async(dispatch_get_main_queue(), ^{
_profileImageView.image = [UIImage imageWithData:data];
});
}
}];
[task resume];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
if(_profileImageView.image == [_detailsDictionary valueForKey:@"ProfilePictureUrl"]) {
NSLog(@"Th url of image is %@",[_detailsDictionary valueForKey:@"ProfilePictureUrl"]);
}
else {
_profileImageView.image = image;
UIImage *updatedImage = _profileImageView.image;
NSData *imageData = UIImageJPEGRepresentation(updatedImage, 100);
NSString *strEncoded = [imageData base64EncodedStringWithOptions:0];
[_detailsDictionary setObject:strEncoded forKey:@"ProfilePictureUrl"];
[self dismissViewControllerAnimated:YES completion:nil];
}
}
要比較的圖像,你需要先下載URL中的形象,創造它的NSData的,將其轉換爲哈希值。對你選擇的本地圖像做同樣的處理。現在比較哈希,如果它們相同,則圖像相同。 – NSNoob
或者,您可以將服務器上的圖像命名爲設備ID和圖像的本地標識符的哈希,然後將其包含在圖片url中。將url中的哈希字符串與選定圖像的localIdentifier和設備ID的生成哈希進行比較。如果它們相同,則圖像相同。我會親自去用第二種方法,因爲它節省了我懶得去下載我其實已經存儲在緩存也圖像的圖像第一 – NSNoob