【发布时间】:2013-06-10 12:44:27
【问题描述】:
尝试异步加载图像我收到此错误 “由于未捕获的异常 '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];
});
});
}
请告知我在哪里出错了。 谢谢 马尤尔
【问题讨论】:
-
这一行发生了什么:
record = [_array objectAtIndex:indexPath.row];?此时,变量record持有对来自_array的某个对象的引用,并且您不再对新创建的NSMutableArray有任何引用。 -
@Theo 实际上我确实有该数组的参考..
-
对不起,我的意思是你的
NSMutableDictionary没有引用了。
标签: ios objective-c nsmutabledictionary