将数组URL转换为xcode中的图像URL数组

时间:2013-03-12 04:02:27

标签: xcode json ios6 nsarray nsdate

我有一个URL数组,我通过JSON解析得到了这个数组

    id=[s valueForKey:@"bid"];

    title=[s valueForKey:@"btitle"];

   author=[s valueForKey:@"author"];

    price=[s valueForKey:@"price"];

    image=[s valueForKey:@"image"];

这里用于显示图像数组(带有URL的数组到tableviewcell)我将该数组转换为该单元格内的NSdata - 用于行索引()方法,如

NSData * imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[image objectAtIndex:indexPath.row]]];

所以我用imagedata在tableviewcell中显示图像..但是无论如何都要以数组格式显示内容,以便我可以轻松使用它。

我想要以下格式使用相同的数组,

NSARRAY *a=[[ NSARRAY alloc]initWithObjects:[UIimage imagenamed:@"url1"],[UIimage imagenamed:@"url2"],nil];

有没有办法将该数据格式转换为数组格式? 请帮帮我

2 个答案:

答案 0 :(得分:1)

使用此代码将图像数据存储到数组中......

NSMutableArray *imagesArray = [[NSMutableArray alloc] init];
    for(int i = 0; i < [image count]; i++)
    {
        NSData *imageData= [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[image objectAtIndex:i]]];
        [imagesArray addObject:[UIImage imageWithData:imageData]];
    }

<强>编辑:

你必须使用这段代码,你从图书中获取图片网址而不是在cellForRowAtIndexPath()方法中...(简单地说,在到达cellForRow ..()方法之前你需要这样做

答案 1 :(得分:0)

使用Linq-to-ObectiveC select method,您可以将字符串数组转换为图像数组,如下所示:

NSArray* images = [urls select:^id(id url) {
    return [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
}];