2016-11-03 2 views
0

ViewController A의 사전에서 URL로 이미지를 가져오고 해당 사전을 ViewController BI에 전달했습니다. 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에 포함시킬 수 있습니다. 선택한 이미지의 localIdentifier 및 장치의 ID에 대해 url의 해시 문자열과 생성 된 해시를 비교합니다. 동일하면 이미지가 동일합니다. 처음에는 이미지를 다운로드하는 번거 로움을 덜어주기 때문에 개인적으로 두 번째 방법을 사용했습니다 – NSNoob

답변

0

문제는이 줄 것 같다 :

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

당신은 _profileImageView.image을 비교하기 위해 노력하고있다 [_detailsDictionary valueForKey:@"ProfilePictureUrl"]로, UIImage 것입니다, 사전에서 오는 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

관련 문제