UIImageView到UITableViewCell

时间:2014-07-17 07:25:10

标签: ios objective-c uitableview uiimage nsmutablearray

我为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中没有显示图像所以请告诉我有关我的问题的任何想法。 谢谢高级

2 个答案:

答案 0 :(得分:1)

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

例如:

-(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加载图像。

答案 1 :(得分:0)

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

 yourTableView.dataSource = self;//or your object

 [yourTableView reloadData];

将使表视图刷新数据。

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