Objective c 无法在iOS中显示表格单元格左侧的缩略图
我在这里看到了一些关于这方面的帖子,但这些解决方案似乎对我不起作用。可能是因为我通过JSON获取图像URL。JSON中的所有其他文本字段都通过了OK,这只是我无法显示的图像,并出现SIGABRT错误 有关守则如下:Objective c 无法在iOS中显示表格单元格左侧的缩略图,objective-c,ios,nsstring,uiimage,nsdata,Objective C,Ios,Nsstring,Uiimage,Nsdata,我在这里看到了一些关于这方面的帖子,但这些解决方案似乎对我不起作用。可能是因为我通过JSON获取图像URL。JSON中的所有其他文本字段都通过了OK,这只是我无法显示的图像,并出现SIGABRT错误 有关守则如下: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier =
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"PostCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *post = [posts objectAtIndex:indexPath.row];
NSString *postText = [post objectForKey:@"post_text"];
NSString *postAuthorName = [post objectForKey:@"post_author_name"];
NSString *postPictureUrl = [post objectForKey:@"post_picture"];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:postPictureUrl]];
cell.textLabel.text = postText;
cell.detailTextLabel.text = [NSString stringWithFormat:@"by %@", postAuthorName];
cell.imageView.image = [UIImage imageWithData:data];
return cell;
}
我的表格单元格是副标题,没有其他更改或接线
知道我把事情搞砸了吗
PS我的完整代码使用async
请求
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"PostCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *post = [posts objectAtIndex:indexPath.row];
NSString *postText = [post objectForKey:@"post_text"];
NSString *postAuthorName = [post objectForKey:@"post_author_name"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *postpictureUrl = [post objectForKey:@"http://example.com/post/1200"];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:postpictureUrl]];
// NSLog(@"%@", data);
dispatch_async(dispatch_get_main_queue(), ^{
// [[cell imageView] setImage:[UIImage imageWithData:data]];
});
});
cell.textLabel.text = postText;
cell.detailTextLabel.text = [NSString stringWithFormat:@"by %@", postAuthorName];
return cell;
}
您可以通过以下方式执行此操作:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
[NSThread detachNewThreadSelector:@selector(downloadImages:) toTarget:self withObject: [NSMutableDictionary dictionaryWithObjectsAndKeys:imageURL,@"imageURL",[NSIndexPath indexPathWithIndex:indexPath.row],@"indexPath", nil]];
调用上述函数和
- (void) downloadImages:(NSMutableDictionary*)dict{
NSURL *nurl = [NSURL URLWithString:[dict objectForKey:@"imageURL"]];
NSURLRequest *req = [NSURLRequest requestWithURL:nurl];
NSURLResponse * response;
NSData *data = [NSURLConnection sendSynchronousRequest: req returningResponse: &response error: &error];
UITableViewCell *cell = [YOURTABLENAME cellForRowAtIndexPath:(NSIndexPath*)[dict objectForKey:@"indexPath"]];
cell.imageView.image = [UIImage imageWithData:data];
if (error) {...handle the error}
}
一些小改动和下面的工作(即,缩略图显示在标题和副标题的左侧) 注意使用了
initWithStyle:UITableViewCellStyleSubtitle
而不是Default
这里的指南是一本重要的读物:
最后,请注意,这是同步下载图像,在现实世界中不起作用。
因此,我将在So上发布另一个关于此问题的最佳解决方案的问题。出于调试目的,您是否可以尝试使用
dataWithContentsOfURL:options:error:
而不是dataWithContentsOfURL
?可能您期望的图像类型无法正确读取或转换为UIImage
对象。另一方面,我认为dataWithContentsOfURL
会阻塞UI,直到它返回。这就是您想要的良好滚动速度吗?john SDK在我使用[NSData DATA WITH CONTENTS OFURL:options:error:[NSURL URLWithString:pictureUrl]]
后期图片URL时抛出了一个错误?它是带有有效图像的有效URL吗?是的,它是类似http://img.youtube.com/vi/dbf-d8Y--no/0.jpg
有多少条这样的记录?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSDictionary *post = [posts objectAtIndex:indexPath.row];
cell.textLabel.text = [post objectForKey:@"post_text"];
cell.detailTextLabel.text = [post objectForKey:@"post_author_name"];
NSString *postpictureUrl = [post objectForKey:@"picture"];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:postpictureUrl]];
cell.imageView.image = [UIImage imageWithData:data];
return cell;
}