如何从数组中提取网址并在表视图上显示图像

问题描述:

在此先感谢。我在数组中获取图像URL。如何从数组中提取网址并在表视图上显示图像

myarray = { "http://www.abc.com/uploads/sarab.jpg", .... }; 

现在我不知道如何访问表视图中的图像?有没有帮助的示例代码?

把这个代码的cellForRowAtIndexPath然后将正常工作:

NSData *imageData= [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[myarray objectAtIndex:indexPath.row]]]; 
cell.imageView.image=[UIImage imageWithData:imageData]; 
+0

谢谢你帮我.......问题就解决 –

你应该做这个,

从阵列将这个代码的cellForRowAtIndexPath获取URL,这将帮助你..

NSString *str = [YOUR_ARR objectAtIndex:indexPath.row]; 
NSURL *url = [NSURL URLWithString:str]; 

首先转换网址NSData的分配,然后再数据你的形象..

NSData *imgData = [NSData dataWithContentsOfURL:url]; 
UIImage *image = [UIImage imageWithData:imgData]; 

,之后

cell.imageView.image = image; 

编码愉快..

+0

谢谢你帮我 –

下面的方式,就可以实现这一点:

NSString *strImageUrl = [myarray objectAtIndex:indexpath.row]; 
NSURL *url = [NSURL URLWithString: strImageUrl]; 
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]]; 
UIImageView *displayimage = [[UIImageView alloc] init]; 
[displayimage setImage:image]; 

让我知道的情况下,任何困难。

干杯

+0

如果我有一些字符串和图像的URL数组,那么我怎么能不使用索引号的获取图像URL阵列。像myarray = {“india”,123654,“http://www.abc.com/uploads/sarab.jpg”,..} –

+0

我认为你已经管理你的阵列很奇怪。如果字符串中存在“www”或“.jpg,.png”,则可以从数组中获取对象,然后从字符串中检查该对象。所以它会像URL一样,你可以绑定你的图片。 –

+0

我的建议是创建NSObject类具有像标题,数字,ImageUrl(印度,123456,abc.com/uploads.srarb.jpg)属性。并将这个类的对象添加到你的数组中。所以,你将通过使用“Index.Row”来获得对象。然后你可以像使用classObject.ImageUrl一样使用imageUrl。并做这个过程。希望你能得到答案。 –