2016-11-03 15 views
0

我得到的圖像作爲從視圖控制器字典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]; 
     } 
    } 
+0

要比較的圖像,你需要先下載URL中的形象,創造它的NSData的,將其轉換爲哈希值。對你選擇的本地圖像做同樣的處理。現在比較哈希,如果它們相同,則圖像相同。 – NSNoob

+0

或者,您可以將服務器上的圖像命名爲設備ID和圖像的本地標識符的哈希,然後將其包含在圖片url中。將url中的哈希字符串與選定圖像的localIdentifier和設備ID的生成哈希進行比較。如果它們相同,則圖像相同。我會親自去用第二種方法,因爲它節省了我懶得去下載我其實已經存儲在緩存也圖像的圖像第一 – NSNoob

回答

0

的問題似乎是在這一行:

if(_profileImageView.image == [_detailsDictionary valueForKey:@"ProfilePictureUrl"]) { 

您正在嘗試比較_profileImageView.image,什麼是UIImage,與[_detailsDictionary valueForKey:@"ProfilePictureUrl"],什麼是NSURL例如,從字典中來。

你能做什麼,而不是,是檢查是否攝取的圖像和profileImage是一樣的。

if(_profileImageView.image == image) { 
// etc.. 

要清除以前緩存圖像,只要致電:

[[NSURLCache sharedURLCache] removeAllCachedResponses]; 

希望這有助於!

+0

我。怎麼可以清除緩存? – FreshStar

+0

什麼緩存?你能顯示一些代碼嗎? – dirtydanee

1

@Dirtydanee,他絕對是正確的,你正在做的URL和的UIImage之間是不兼容的比較。所以請用下面的代碼糾正這個問題。

NSData *data1 = UIImagePNGRepresentation(previousImage); 
NSData *data2 = UIImagePNGRepresentation(currentImage); 

if([data1 isEqualToData:data2]) { 
    //Do something 
} else { 
    //Do something 
} 

將圖像轉換成NSData並比較數據。 如果你想逐位比較請看以下鏈接:

Generate hash from UIImage

相關問題