2012-10-30 41 views
2

我有一个包含图片网址的数组,我想用这些网址创建一个图片视图(现在用for循环)。如何用urls数组填充UIImageView?

,但我没有得到一个想法,如何使图像视图与在阵列 希望网址,有些人会澄清这

int x = 0; 
for(int i = 0; i <imagearry.count; i ++){ 
NSDictionary *dict = [imagearry objectAtIndex:i]; 
NSLog(@"%@",[dict objectForKey:@"img_url"]); 
NSString *filePath [NSHomeDirectory()stringByAppendingPathComponent:@"/Library/Caches"]; 

    stringarry = [[imagearry objectAtIndex:i] objectForKey:@"img_url"]; 

    int x = 0; 

    imageView.image = [UIImage imageNamed:[stringarry objectAtIndex:0]]; 

    for(int i=0; i < [stringarry count]; i++) 
    { 

     UIImageView *img = [[UIImageView alloc] init]; 
     img.bounds = CGRectMake(10, 10, 50, 50); 
     img.frame = CGRectMake(5+x, 0, 160, 110); 
     NSLog(@"image: %@",[stringarry objectAtIndex:i]); 


     img.image = [UIImage imageNamed:[stringarry objectAtIndex:i]]; 
     [stringarry insertObject:img atIndex:i]; 
     [scrollView addSubview:[stringarry objectAtIndex:i]]; 

     x += 170; 
+0

没有“我要创建这些网址的形象图”是什么意思吗?如果想要为每个URL创建一个ImageView,然后查看Three20库的TTImageView。 –

回答

0

UIImageView没有属性来显示URL中的图像。因此 您可能必须以NSDATA格式下载它,然后转换为 UIImage。相反,请查看AsyncImageView并将AsyncImageView的实例 存储在您的数组中。

检查此链接:AsyncImageView

+0

它确实有一个属性,我想.. UIImage * image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:MyURL]]]; –

+0

yes..true多数民众赞成在我刚才提到的答案..但这种做法是同步的,因此将阻止主UI,除非UIImages下载在阵列中..因此AsyncImageView将继续异步下载 – AppleDelegate

+0

如果图像在磁盘是更好的imageWithData选项,如果他们是来自互联网的图像使用AsyncImageView。 – jcesarmobile

0

你必须使用imageWithData而不是imageNamed

NSURL * url = [NSURL fileURLWithPath:[filePath stringByAppendingPathComponent:[stringarry objectAtIndex:i]]]; 
[UIImage imageWithData:[NSData dataWithContentsOfURL:url]];