0

I am getting image as url from dictionary on ViewController A and I have passed that dictionary to ViewController B.I want that if the user has updated the image then it shows the updated image else it shows the previous image and I am doing the following code for it .Kindly check and tell why is it not working as desired and showing the previous image only in every case.

-(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];
        }
    }
kb920
  • 3,039
  • 2
  • 33
  • 44
FreshStar
  • 193
  • 1
  • 1
  • 10
  • To compare the images, you need to download the image from the URL first, create it's NSData, convert it to hash. Do the same with the local image you selected. Now compare the hashes, if they are same, the images are same. – NSNoob Nov 03 '16 at 09:07
  • Alternatively, you can name the image on your server to be a hash of the device ID and local identifier of the image and then include it in the picture url. Compare the hash string in url with the generated hash for selected image's localIdentifier and device's id. If they are the same, images are same. I would personally go with the second approach as it saves me the bother to download the image first – NSNoob Nov 03 '16 at 09:08

2 Answers2

1

@Dirtydanee, He is absolutely correct, you are doing incompatible comparison between Url and UIImage. So please correct this with following code.

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

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

Convert images into NSData and compare the data. If you want bit-by-bit comparison Please look at the following link:

Generate hash from UIImage

Community
  • 1
  • 1
0

The problem seems to be in this line:

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

You are trying to compare the _profileImageView.image, what is UIImage, with [_detailsDictionary valueForKey:@"ProfilePictureUrl"], what is NSURL instance, coming from the dictionary.

What you could do instead, is checking if the picked image and the profileImage is the same.

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

To clear previously cached images, just call:

[[NSURLCache sharedURLCache] removeAllCachedResponses];

Hope this helps!

dirtydanee
  • 6,081
  • 2
  • 27
  • 43