0

尝试异步加载图像我收到此错误“由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[valueForUndefinedKey:]:此类不符合键img的键值编码。” 我的代码是以下

 NSString *str = "URL of an image";

    NSMutableDictionary *record = [NSMutableDictionary dictionary];
    [record setObject:str forKey:@"img"];

    record = [_array objectAtIndex:indexPath.row];



   dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,  0ul);
    if ([record valueForKey:@"img"]) {
        NSLog(@"in if");
        cell.m_img.image =  [record valueForKey:@"img"];
    } else {
        dispatch_async(queue, ^{
             NSLog(@"in else");
            NSURL  *imageurl = [[NSURL alloc] initWithString:str];
            NSData *image = [[NSData alloc] initWithContentsOfURL:imageurl];

            dispatch_async(dispatch_get_main_queue(), ^{
                [record setValue:[UIImage imageWithData:image] forKey:@"img"];

                [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
            });
        });
    }

请告知我在这里哪里出错了。谢谢马尤尔

4

4 回答 4

1
 Check if object in array is of dictionary type

  NSString *str = "URL of an image";

    NSMutableDictionary *record = [NSMutableDictionary dictionary];

if([[_array objectAtIndex:indexPath.row]isKindOfClass:[NSDictionary class]])
    record = [_array objectAtIndex:indexPath.row];
else
      [record setObject:str forKey:@"img"];


   dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,  0ul);
    if ([record valueForKey:@"img"]) {
        NSLog(@"in if");
        cell.m_img.image =  [record valueForKey:@"img"];
    } else {
        dispatch_async(queue, ^{
             NSLog(@"in else");
            NSURL  *imageurl = [[NSURL alloc] initWithString:str];
            NSData *image = [[NSData alloc] initWithContentsOfURL:imageurl];

            dispatch_async(dispatch_get_main_queue(), ^{
                [record setValue:[UIImage imageWithData:image] forKey:@"img"];

                [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
            });
        });
    }
于 2013-06-10T13:08:15.850 回答
1

代替

[record valueForKey:@"img"]

[record objectForKey:@"img"]
于 2013-06-10T12:47:27.373 回答
0

在您的评论和答案的帮助下解决了问题。我所做的是在 Globally used 类中创建 NSMutable Dictionary 并初始化变量。谢谢大家的帮助

arr = [[NSMutableDictionary alloc] init];


dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,  0ul);
    if ([[[DataManager sharedInstance] arr] objectForKey:str]) {
        NSLog(@"in if");

         [cell m_img].image = [UIImage imageWithData:[[[DataManager sharedInstance] arr] objectForKey:str]]; 
    } else {
        dispatch_async(queue, ^{
             NSLog(@"in else");



                NSURL  *imageurl = [[NSURL alloc] initWithString:str];
                NSData *image = [[NSData alloc] initWithContentsOfURL:imageurl];
                [[[DataManager sharedInstance] arr] setObject :image forKey:str];
                [cell m_img].image = [UIImage imageWithData:[[[DataManager sharedInstance] arr] objectForKey:str]]; 

        });
    }
于 2013-06-11T06:54:08.027 回答
0

这条线很可能

[记录valueForKey:@"img"]

objectForKey:如果未设置键 img 将返回 nil。在您的情况下是更好的选择。

但是,请总体检查您的代码。您首先创建一个新的NSMutableDictionary并将其分配给record. 然后将一个字符串对象添加到该字典中。到目前为止还好。之后,您也从分配给的数组 _array 中获取一个对象record。到那时,您不再对新创建的字典有任何引用。是不是故意的。如果您不使用 ARC,那么您会在此处创建内存泄漏。

编辑以回应以下评论:

NSMutableDictionary *record = [NSMutableDictionary dictionary]; // here you create a new empty instance of NSMutableDictionary and store it in record. It has a retain count of 1
[record setObject:str forKey:@"img"]; // here you add an object to that new dictionary. Now it has exactly one entry with the key "img"
record = [_array objectAtIndex:indexPath.row]; //and here you discard the retained newly creaded dictionary by assigning another one from the array _array. What is the purpose of that?
于 2013-06-10T12:51:30.703 回答