如何从服务器导入图像并将其排列在视图中

我需要获取必须从服务器调用的图像数组并按顺序排列。但是执行下面的代码我无法得到它…指导请…

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view { NSURL *url = [NSArray arrayWithObjects:[NSURL URLWithString: @"http://sofzh.miximages.com/iphone/The-hulk-2003.jpg"], [NSURL URLWithString:@"http://sofzh.miximages.com/iphone/ios62.jpg"], [NSURL URLWithString:@"http://sofzh.miximages.com/iphone/google_logo.jpg"], [NSURL URLWithString:@"http://sofzh.miximages.com/iphone/Google-doodle-of-Richard-007.jpg"], [NSURL URLWithString:@"http://sofzh.miximages.com/iphone/apple-vs-google_2.jpg"], [NSURL URLWithString:@"http://sofzh.miximages.com/iphone/IronMan_9_wallpaper.jpg"], [NSURL URLWithString:@"http://sofzh.miximages.com/iphone/iphone_5.jpg"],Nil]; /* int i=0; int cord_x=0; int cord_y=30; int cont=0; for (i = 0; i  3) { cord_x=0; cord_y=cord_y+110; cont=1; }*/ UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:(NSURL*)[(NSArray*)url objectAtIndex:index]]]; //cord_x=cord_x+110; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(0, 0, 200.0f, 200.0f); [button setImage:(UIImage*)[image objectAtIndex:index] forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; return button; } //} 

现在我需要从服务器放置按钮图像和图像…

  int row = 0; int column = 0; for(int i = 0; i < url.count; ++i) { UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:(NSURL*) [(NSArray*)url objectAtIndex:i]]]; UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(column*100+16, row*90, 80, 80); [button setImage:image forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; button.tag = i; [self.view addSubview:button]; if (column == 2) { column = 0; row++; } else { column++; } } 

您可以尝试使用NSURLConnection异步下载图像,这将使您的UI更具响应性。 只需为每个图像设置单独的连接,并在委托方法 – connectionDidFinishLoading中更新按钮上的图像。 它还可以帮助您跟踪下载错误(例如超时等)。 下面是表格下载图片的一个很好的例子 – 你可以找到很多帮助你的案例: http : //developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html

 NSMutableArray *arrayList=[[NSMutableArray alloc] initWithObjects:@"www.xxx.com/imag.jpg",@"www.xxx.com/img1.jpg",nil]; for(int i=0; i<[arrayList count]; i++) { NSURL *url = [NSURL URLWithString:[arrayList ObjetAtIndex:i]]; NSData *data = [NSData dataWithContentsOfURL: url]; UIImage *img=[UIImage imageWithData:data]; // Here is your image } 
 NSURL *url = [NSArray arrayWithObjects:[NSURL URLWithString:@"http://sofzh.miximages.com/iphone/logo-iphone-dev-tips.png"],[NSURL URLWithString:@"http://sofzh.miximages.com/iphone/ios62.jpg"],[NSURL URLWithString:@"http://sofzh.miximages.com/iphone/ios62.jpg"],[NSURL URLWithString:@"http://sofzh.miximages.com/iphone/ios62.jpg"],[NSURL URLWithString:@"http://sofzh.miximages.com/iphone/ios62.jpg"],[NSURL URLWithString:@"http://sofzh.miximages.com/iphone/ios62.jpg"],[NSURL URLWithString:@"http://sofzh.miximages.com/iphone/ios62.jpg"],Nil]; int i=0; int cord_x=0; int cord_y=30; int cont=0; for (i = 0; i < [(NSArray*)url count]; i++) { cont=cont+1; if (cont > 3) { cord_x=0; cord_y=cord_y+110; cont=1; } UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:(NSURL*)[(NSArray*)url objectAtIndex:i]]]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(cord_x, cord_y, 100, 100)]; [imageView setImage:image]; [self.view addSubview:imageView]; cord_x=cord_x+110; }