自定义表视图单元格中的JSON缩略图图像

时间:2015-04-08 16:29:50

标签: ios json image parsing

我真的厌倦了尝试解决这个问题,所以我觉得我需要一些帮助。我正在从我成功解析的API中填充iOS的每个单元格(可以检索简单的东西,如文本或数字,没有问题) 但是当我尝试为缩略图设置图像时,我遇到了一些错误。

这是我的代码:

NSString *imageUrl = [tempDictionary valueForKeyPath:@"files.url_thumb"];
NSURL *url = [NSURL URLWithString:imageUrl];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *thumbNailImage = [UIImage imageWithData:imageData];

dispatch_async(dispatch_get_main_queue(), ^{


    [cell.ThumbImage setImage:thumbNailImage];
});

这是我控制台中的噩梦:

2015-04-08 13:14:26.450 WebTableView[4976:173380] -[__NSArrayI length]: 

unrecognized selector sent to instance 0x79e18b90
2015-04-08 13:14:26.509 WebTableView[4976:173380] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x79e18b90'
*** First throw call stack:
(
    0   CoreFoundation                      0x036c9466 __exceptionPreprocess + 182
    1   libobjc.A.dylib                     0x03352a97 objc_exception_throw + 44
    2   CoreFoundation                      0x036d12c5 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
    3   CoreFoundation                      0x03619bc7 ___forwarding___ + 1047
    4   CoreFoundation                      0x0361978e _CF_forwarding_prep_0 + 14
    5   CoreFoundation                      0x035836cf CFStringGetLength + 143
    6   CoreFoundation                      0x036a9c8d _CFURLCreateWithURLString + 77
    7   CoreFoundation                      0x035952d3 CFURLCreateWithString + 35
    8   Foundation                          0x02f17999 -[NSURL(NSURL) initWithString:relativeToURL:] + 371
    9   Foundation                          0x02f17807 +[NSURL(NSURL) URLWithString:relativeToURL:] + 80
    10  Foundation                          0x02f177b1 +[NSURL(NSURL) URLWithString:] + 48
    11  WebTableView                        0x00111a8b -[TableViewController tableView:cellForRowAtIndexPath:] + 571
    12  UIKit                               0x01c26c9c -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 473
    13  UIKit                               0x01c26d7e -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 77
    14  UIKit                               0x01c0054b -[UITableView _updateVisibleCellsNow:isRecursive:] + 3034
    15  UIKit                               0x01c1aeb1 -[UITableView layoutSubviews] + 222
    16  UIKit                               0x01b907b1 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 608
    17  libobjc.A.dylib                     0x03368771 -[NSObject performSelector:withObject:] + 70
    18  QuartzCore                          0x009421cf -[CALayer layoutSublayers] + 152
    19  QuartzCore                          0x00936055 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 397
    20  QuartzCore                          0x00935eb0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
    21  QuartzCore                          0x008941b6 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 284
    22  QuartzCore                          0x0089558a _ZN2CA11Transaction6commitEv + 392
    23  QuartzCore                          0x00895c56 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
    24  CoreFoundation                      0x035ec18e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
    25  CoreFoundation                      0x035ec0d0 __CFRunLoopDoObservers + 400
    26  CoreFoundation                      0x035e1b0a __CFRunLoopRun + 1226
    27  CoreFoundation                      0x035e137b CFRunLoopRunSpecific + 443
    28  CoreFoundation                      0x035e11ab CFRunLoopRunInMode + 123
    29  GraphicsServices                    0x04b3d2c1 GSEventRunModal + 192
    30  GraphicsServices                    0x04b3d0fe GSEventRun + 104
    31  UIKit                               0x01b049b6 UIApplicationMain + 1526
    32  WebTableView                        0x000dd6cd main + 141
    33  libdyld.dylib                       0x03e94ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

1 个答案:

答案 0 :(得分:0)

我认为valueForKeyPath:返回一个对象数组。尝试记录imageUrl并检查你得到了什么。

如果您确信只有一个网址,那么您可以试试这个 -

NSString *imageUrl = [[tempDictionary valueForKeyPath:@"files.url_thumb"] lastObject];

否则尝试这样的事情 -

valueForKey:@"files"] valueForKey:@"url_thumb]