IOS:将图像从 parse.com 加载到表视图中
IOS: Loading image from parse.com into a tableview
我正在尝试将关联图像从解析加载到自定义 table 单元格中。当我执行代码时,解析被成功查询并且 table 单元格的其他信息(如书名和作者)正确显示,但是当我尝试添加图像时,应用程序崩溃了。
这是我用来获取图像数据的代码。
-(void)queryForNewBooks{
_bookNameArray = [[NSMutableArray alloc] init];
_authorNameArray = [[NSMutableArray alloc] init];
_isbnNumberArray = [[NSMutableArray alloc] init];
_bookImageData = [[NSMutableArray alloc] init];
PFQuery *query = [PFQuery queryWithClassName:@"BooksForSale"];
[query orderByDescending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^( NSArray *objects, NSError *error) {
if (objects.count >=1) {
for (PFObject *object in objects) {
PFFile *imageFile = [object objectForKey:@"image"];
[imageFile getDataInBackgroundWithBlock:^(NSData *result, NSError *error) {
if (!error) {
NSData *data = result;
NSLog(@"HEYYYYYY");
[ _bookImageData addObject:data];
}
}];
这是table单元格代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"TableViewCell";
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSLog(@"Here are the book names, %@",_bookNameArray);
cell.bookNameLabel.text = [_bookNameArray objectAtIndex:indexPath.row];
cell.authorNameLabel.text = [_authorNameArray objectAtIndex:indexPath.row];
UIImage *image = [UIImage imageWithData: [_bookImageData objectAtIndex:indexPath.row]];
cell.bookImageLabel.image = image;
return cell;
}
我认为是这两行导致了问题
UIImage *image = [UIImage imageWithData: [_bookImageData objectAtIndex:indexPath.row]];
cell.bookImageLabel.image = image;
当我评论这两行时,应用程序不会崩溃。
任何人都可以就我做错了什么提出建议吗?
谢谢
错误日志:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
*** First throw call stack:
(
0 CoreFoundation 0x000000010f951f45 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010f3cbdeb objc_exception_throw + 48
2 CoreFoundation 0x000000010f8359e4 -[__NSArrayM objectAtIndex:] + 212
3 ParseStarterProject 0x000000010c45fd41 -[ParseStarterProjectViewController tableView:cellForRowAtIndexPath:] + 753
4 UIKit 0x000000010dba9e2a -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 782
5 UIKit 0x000000010dba9f3f -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74
6 UIKit 0x000000010db7f307 -[UITableView _updateVisibleCellsNow:isRecursive:] + 3187
7 UIKit 0x000000010dbb2d1c -[UITableView _performWithCachedTraitCollection:] + 92
8 UIKit 0x000000010db9a884 -[UITableView layoutSubviews] + 223
9 UIKit 0x000000010db08e40 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 710
10 QuartzCore 0x000000010cf4559a -[CALayer layoutSublayers] + 146
11 QuartzCore 0x000000010cf39e70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
12 QuartzCore 0x000000010cf39cee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
13 QuartzCore 0x000000010cf2e475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
14 QuartzCore 0x000000010cf5bc0a _ZN2CA11Transaction6commitEv + 486
15 QuartzCore 0x000000010cf5c37c _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
16 CoreFoundation 0x000000010f87d947 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
17 CoreFoundation 0x000000010f87d8b7 __CFRunLoopDoObservers + 391
18 CoreFoundation 0x000000010f87350b __CFRunLoopRun + 1147
19 CoreFoundation 0x000000010f872e08 CFRunLoopRunSpecific + 488
20 GraphicsServices 0x0000000110f79ad2 GSEventRunModal + 161
21 UIKit 0x000000010da5430d UIApplicationMain + 171
22 ParseStarterProject 0x000000010c45dfbf main + 111
23 libdyld.dylib 0x00000001114fa92d start + 1
24 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
数据似乎没有被添加到 nsmutable数组中,而这应该出现在这里
for (PFObject *object in objects) {
PFFile *imageFile = [object objectForKey:@"image"];
[imageFile getDataInBackgroundWithBlock:^(NSData *result, NSError *error) {
if (!error) {
NSData *data = result;
NSLog(@"HEYYYYYY");
[ _bookImageData addObject:data];
}
getDataInBackgroundWithBlock
是一个异步进程。
它需要时间来接收响应。所以在请求完成之前,你的 _bookImageData 是空数组。
在 _bookImageData 有数据之前,- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
正在被调用,结果你的应用崩溃了!
UIImage *image = [UIImage imageWithData: [_bookImageData objectAtIndex:indexPath.row]];
cell.bookImageLabel.image = image;
解决方案是
① 在收到 _bookImageData 后尝试通过以下方式刷新您的 tableview:[yourtableview reloadData] 或类似的东西。
② 在调用 objectAtIndex 之前使用 try..catch 或检查 _bookImageData 使您的应用程序不崩溃。
建议:使用一些占位符图片让您的应用更酷。 :)
@try {
UIImage *image = [UIImage imageWithData: [_bookImageData objectAtIndex:indexPath.row]];
cell.bookImageLabel.image = image;
} catch ...
我正在尝试将关联图像从解析加载到自定义 table 单元格中。当我执行代码时,解析被成功查询并且 table 单元格的其他信息(如书名和作者)正确显示,但是当我尝试添加图像时,应用程序崩溃了。
这是我用来获取图像数据的代码。
-(void)queryForNewBooks{
_bookNameArray = [[NSMutableArray alloc] init];
_authorNameArray = [[NSMutableArray alloc] init];
_isbnNumberArray = [[NSMutableArray alloc] init];
_bookImageData = [[NSMutableArray alloc] init];
PFQuery *query = [PFQuery queryWithClassName:@"BooksForSale"];
[query orderByDescending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^( NSArray *objects, NSError *error) {
if (objects.count >=1) {
for (PFObject *object in objects) {
PFFile *imageFile = [object objectForKey:@"image"];
[imageFile getDataInBackgroundWithBlock:^(NSData *result, NSError *error) {
if (!error) {
NSData *data = result;
NSLog(@"HEYYYYYY");
[ _bookImageData addObject:data];
}
}];
这是table单元格代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"TableViewCell";
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSLog(@"Here are the book names, %@",_bookNameArray);
cell.bookNameLabel.text = [_bookNameArray objectAtIndex:indexPath.row];
cell.authorNameLabel.text = [_authorNameArray objectAtIndex:indexPath.row];
UIImage *image = [UIImage imageWithData: [_bookImageData objectAtIndex:indexPath.row]];
cell.bookImageLabel.image = image;
return cell;
}
我认为是这两行导致了问题
UIImage *image = [UIImage imageWithData: [_bookImageData objectAtIndex:indexPath.row]];
cell.bookImageLabel.image = image;
当我评论这两行时,应用程序不会崩溃。
任何人都可以就我做错了什么提出建议吗?
谢谢
错误日志:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' *** First throw call stack: ( 0 CoreFoundation 0x000000010f951f45 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x000000010f3cbdeb objc_exception_throw + 48 2 CoreFoundation 0x000000010f8359e4 -[__NSArrayM objectAtIndex:] + 212 3 ParseStarterProject 0x000000010c45fd41 -[ParseStarterProjectViewController tableView:cellForRowAtIndexPath:] + 753 4 UIKit 0x000000010dba9e2a -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 782 5 UIKit 0x000000010dba9f3f -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74 6 UIKit 0x000000010db7f307 -[UITableView _updateVisibleCellsNow:isRecursive:] + 3187 7 UIKit 0x000000010dbb2d1c -[UITableView _performWithCachedTraitCollection:] + 92 8 UIKit 0x000000010db9a884 -[UITableView layoutSubviews] + 223 9 UIKit 0x000000010db08e40 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 710 10 QuartzCore 0x000000010cf4559a -[CALayer layoutSublayers] + 146 11 QuartzCore 0x000000010cf39e70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 12 QuartzCore 0x000000010cf39cee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 13 QuartzCore 0x000000010cf2e475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277 14 QuartzCore 0x000000010cf5bc0a _ZN2CA11Transaction6commitEv + 486 15 QuartzCore 0x000000010cf5c37c _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92 16 CoreFoundation 0x000000010f87d947 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 17 CoreFoundation 0x000000010f87d8b7 __CFRunLoopDoObservers + 391 18 CoreFoundation 0x000000010f87350b __CFRunLoopRun + 1147 19 CoreFoundation 0x000000010f872e08 CFRunLoopRunSpecific + 488 20 GraphicsServices 0x0000000110f79ad2 GSEventRunModal + 161 21 UIKit 0x000000010da5430d UIApplicationMain + 171 22 ParseStarterProject 0x000000010c45dfbf main + 111 23 libdyld.dylib 0x00000001114fa92d start + 1 24 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException
数据似乎没有被添加到 nsmutable数组中,而这应该出现在这里
for (PFObject *object in objects) {
PFFile *imageFile = [object objectForKey:@"image"];
[imageFile getDataInBackgroundWithBlock:^(NSData *result, NSError *error) {
if (!error) {
NSData *data = result;
NSLog(@"HEYYYYYY");
[ _bookImageData addObject:data];
}
getDataInBackgroundWithBlock
是一个异步进程。
它需要时间来接收响应。所以在请求完成之前,你的 _bookImageData 是空数组。
在 _bookImageData 有数据之前,- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
正在被调用,结果你的应用崩溃了!
UIImage *image = [UIImage imageWithData: [_bookImageData objectAtIndex:indexPath.row]];
cell.bookImageLabel.image = image;
解决方案是
① 在收到 _bookImageData 后尝试通过以下方式刷新您的 tableview:[yourtableview reloadData] 或类似的东西。
② 在调用 objectAtIndex 之前使用 try..catch 或检查 _bookImageData 使您的应用程序不崩溃。
建议:使用一些占位符图片让您的应用更酷。 :)
@try {
UIImage *image = [UIImage imageWithData: [_bookImageData objectAtIndex:indexPath.row]];
cell.bookImageLabel.image = image;
} catch ...