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

时间:2012-06-27 06:48:38

标签: iphone ios xcode uiimageview nsurlconnection

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

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{

NSURL *url = [NSArray arrayWithObjects:[NSURL URLWithString: 
                                        @"http://images.wikia.com/marvelmovies/images/5/5e/The-hulk-2003.jpg"],
              [NSURL URLWithString:@"http://cdn.gottabemobile.com/wp-content/uploads/2012/06/ios62.jpg"],
              [NSURL URLWithString:@"http://challenge.roadef.org/2012/img/google_logo.jpg"],
              [NSURL URLWithString:@"http://addyosmani.com/blog/wp-content/uploads/2012/03/Google-doodle-of-Richard-007.jpg"],
              [NSURL URLWithString:@"http://techcitement.com/admin/wp-content/uploads/2012/06/apple-vs-google_2.jpg"],
              [NSURL URLWithString:@"http://www.andymangels.com/images/IronMan_9_wallpaper.jpg"],
              [NSURL URLWithString:@"http://sequelnews.com/wp-content/uploads/2011/11/iphone_5.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: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;

}

//}

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

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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
}

答案 2 :(得分:1)

NSURL *url = [NSArray arrayWithObjects:[NSURL URLWithString:@"http://mobiledevelopertips.com/images/logo-iphone-dev-tips.png"],[NSURL URLWithString:@"http://cdn.gottabemobile.com/wp-content/uploads/2012/06/ios62.jpg"],[NSURL URLWithString:@"http://cdn.gottabemobile.com/wp-content/uploads/2012/06/ios62.jpg"],[NSURL URLWithString:@"http://cdn.gottabemobile.com/wp-content/uploads/2012/06/ios62.jpg"],[NSURL URLWithString:@"http://cdn.gottabemobile.com/wp-content/uploads/2012/06/ios62.jpg"],[NSURL URLWithString:@"http://cdn.gottabemobile.com/wp-content/uploads/2012/06/ios62.jpg"],[NSURL URLWithString:@"http://cdn.gottabemobile.com/wp-content/uploads/2012/06/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;
}

答案 3 :(得分:1)

 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++;
    }
}