【问题标题】:How to Importing image from server and arrange it in a view如何从服务器导入图像并将其排列在视图中
【发布时间】:2012-06-28 13:34:22
【问题描述】:

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

- (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;

}

//}

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

【问题讨论】:

  • 你在浏览器上查看过那些图片吗?
  • 等等,图像没有显示在视图中,还是它们为 NULL?

标签: iphone ios xcode uiimageview nsurlconnection


【解决方案1】:

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

【讨论】:

    【解决方案2】:
     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++;
        }
    }
    

    【讨论】:

    • 你能告诉我是否需要将单个图像排列到我命名为 iv1、iv2 等的单个图像视图中...
    • @DANY 因为你必须设置每个图像视图的帧大小。你想创建任何类型的图像库吗?
    • :是的..但我正在创造就像如果我们触摸图像会导致购买该图像产品...
    • 嘿,我已经创建了网格视图的图片库。如果你需要比告诉我更多的帮助。
    • 是的,我需要...求你的帮助...现在我编辑了这个问题,请检查一下...stackoverflow.com/questions/11220795/…
    【解决方案3】:
    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;
    }
    

    【讨论】:

    • 你能告诉我是否需要将单个图像排列到我命名为 iv1、iv2 等的单个图像视图中...
    • 没必要,让我为你编辑代码。就一秒
    • @Dany 我编辑了代码,在 NSURL *url 中添加了一些 url;这将为您提供缩略图视图中的图像,您可以设置 imageview 的标签,比如 imageView.tag = i;
    【解决方案4】:
    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
    }
    

    【讨论】:

    • 这不起作用,我收到多个警告和 SIGNAL sigabart 问题...
    猜你喜欢
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多