更新图像并将其保存在字典中

时间:2021-10-24 11:18:13

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.

我从ViewController A的字典中获取图像作为url并且我已经将该字典传递给ViewController 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];
        }
    }

2 个解决方案

#1


1  

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

@Dirtydanee,他是绝对正确的,你在Url和UIImage之间进行不兼容的比较。所以请使用以下代码更正此问题。

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:

将图像转换为NSData并比较数据。如果您想逐位比较请查看以下链接:

Generate hash from UIImage

从UIImage生成哈希

#2


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.

你试图比较_profileImageView.image,什么是UIImage,与[_detailsDictionary valueForKey:@“ProfilePictureUrl”],什么是NSURL实例,来自字典。

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

您可以做的是检查拾取的图像和profileImage是否相同。

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

To clear previously cached images, just call:

要清除以前缓存的图像,只需调用:

[[NSURLCache sharedURLCache] removeAllCachedResponses];

Hope this helps!

希望这可以帮助!

#1


1  

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

@Dirtydanee,他是绝对正确的,你在Url和UIImage之间进行不兼容的比较。所以请使用以下代码更正此问题。

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:

将图像转换为NSData并比较数据。如果您想逐位比较请查看以下链接:

Generate hash from UIImage

从UIImage生成哈希

#2


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.

你试图比较_profileImageView.image,什么是UIImage,与[_detailsDictionary valueForKey:@“ProfilePictureUrl”],什么是NSURL实例,来自字典。

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

您可以做的是检查拾取的图像和profileImage是否相同。

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

To clear previously cached images, just call:

要清除以前缓存的图像,只需调用:

[[NSURLCache sharedURLCache] removeAllCachedResponses];

Hope this helps!

希望这可以帮助!