【问题标题】:UIImageView to UITableViewCellUIImageView 到 UITableViewCell
【发布时间】:2014-09-07 22:58:24
【问题描述】:

我得到 JSON 的图像。像这样:-

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSMutableArray *al=[NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
    for (NSDictionary *diction in al)
    {
        NSString *geting =[diction valueForKey:@"dealimage"];
        NSLog(@"dealimage is %@",geting);

        NSData *getdata=[[NSData alloc]initWithData:[NSData dataFromBase64String:geting]];
        NSLog(@"good day %@",getdata);
        dataimages=[UIImage imageWithData:getdata];
        NSLog(@"dataimages %@",dataimages);

        [imagesarray addObject:dataimages];

     }
    NSLog(@"images array is array%@",imagesarray);
     NSLog(@"dataimages 8images %@",dataimages);
}

当我打印图像数组时,它显示如下:-

images array is array(
    "<UIImage: 0x7539b70>",
    "<UIImage: 0x7176e00>",
    "<UIImage: 0x7182960>",
    "<UIImage: 0x7185d40>",
    "<UIImage: 0x7541d00>",
    "<UIImage: 0x7186e30>",
    "<UIImage: 0x7186ff0>",
    "<UIImage: 0x7187410>"
)

我尝试的是 NSString 通过 Base64String 传递给 NSData,然后 NSData 传递给 UIImage,然后 UIImage 传递给 NSMutablearray(imagearray)。现在我将此数组传递给 UITableViewCell 比如:-

cell.imageView.image = [imagesarray objectAtIndex:indexPath.row];

但是图像没有显示在 UITableViewCell 所以请给我关于我的问题的任何想法。 感谢高级

【问题讨论】:

  • 解析完数据后是否重新加载tableView?
  • @Morpheus 开玩笑说我尝试在 UITableViewCell 上显示 Json 图像
  • 如果您在实际解析数据之前已经创建了tableview数据源,那么您必须在得到响应后重新加载数据才能在tableView中显示它
  • 只有一行[yourtableview reloadData];在完成你的 for 循环之后。
  • AFNetworking 将为您提供更多帮助。只需正常调用您的变量即可。以图像数组为例。

标签: ios objective-c uitableview uiimage nsmutablearray


【解决方案1】:

从 Json 加载图像到 Table-view 的方式是错误的。最好使用 imageURL 加载数组并将其图像从 URL 加载到 CellForRowAtIndex: 方法

例如:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSMutableArray *al=[NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
    for (NSDictionary *diction in al)
    {
        NSString *geting =[diction valueForKey:@"dealimage"];
        NSLog(@"dealimage is %@",geting);
        [imagesarray addObject:geting]; // here adding image URL in side the Array

     }  

  [self.tableview reloadData];
}

和 CellForAtIndex:

  cell.imageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[imagesarray objectAtIndex:indexPath.row]]]];

为了获得更好的平滑性能,请在 SDWebImage 或许多其他库的帮助下使用异步图像加载,这些库可以更快地从 url 加载图像。

【讨论】:

  • 我知道我只是建议将带有 url 的图像加载到 CellForRowIndexPath 方法中。
  • @NitinGohel 感谢您的回复,但我按照您所说的进行了尝试,但我没有在 UITableViewCell 上获得图像请告诉我有关我的问题的任何想法
【解决方案2】:
 NSLog(@"images array is array%@",imagesarray);

 yourTableView.dataSource = self;//or your object

 [yourTableView reloadData];

将使表格视图刷新数据。

如果您没有设置数据源,那么表格视图将不会查找要显示的表格数据。

【讨论】:

  • @Vijay-Apple-Dev.blegspot 感谢您的回复请告诉我它把你的代码放在哪里我是新手
  • connectionDidFinishLoading方法最后的代码就是上面的部分
  • @Vijay-Apple-Dev.blegspot [table reloadData] 玩笑放在循环之后我得到了输出谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多