【问题标题】:Updating image and saving it in dictionary更新图像并将其保存在字典中
【发布时间】:2017-03-16 17:21:45
【问题描述】:

我从 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];
        }
    }

【问题讨论】:

  • 要比较图片,你需要先从URL下载图片,创建它的NSData,将其转换为hash。对您选择的本地图像执行相同操作。现在比较哈希值,如果它们相同,则图像相同。
  • 或者,您可以将服务器上的图像命名为图像的设备 ID 和本地标识符的哈希值,然后将其包含在图片 url 中。将 url 中的哈希字符串与为所选图像的 localIdentifier 和设备 ID 生成的哈希进行比较。如果它们相同,则图像相同。我个人会采用第二种方法,因为它省去了我先下载图像的麻烦

标签: ios objective-c iphone uiimageview


【解决方案1】:

问题似乎出在这一行:

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

您正在尝试比较来自字典的_profileImageView.imageUIImage,与[_detailsDictionary valueForKey:@"ProfilePictureUrl"]NSURL 实例。

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

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

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

[[NSURLCache sharedURLCache] removeAllCachedResponses];

希望这会有所帮助!

【讨论】:

  • 我实际上也将图像存储在缓存中。如何清除缓存?
【解决方案2】:

@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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多