1

I want to do lazy loading using an UIScrollview and want to add some photos which come as a response from a web service.

The code I have written so far is as follows:

for (int j=0;j<9;j++) {
        for (int i=0; i<[mainRestaurantArray count];i++) {  //**mainRestaurantArray is the array which has the uiimage in one of it index**
            if ([[[mainRestaurantArray objectAtIndex:i] valueForKey:@"Image"] isKindOfClass:[UIImage class]]) {     
                [CombineArray addObject:[mainRestaurantArray objectAtIndex:i]]; 
                //NSLog(@"cnt=>%d   array==>%@",cnt,[CombineArray objectAtIndex:cnt]);


                UIButton* btn = [[UIButton alloc]init];
                btn.tag = cnt;
                btn.frame = CGRectMake(15+(cnt%5)*60, 15+(cnt/5)*60,Width,Height);
                btn.backgroundColor = [UIColor greenColor];
                [btn setBackgroundImage:[[CombineArray objectAtIndex:cnt] valueForKey:@"Image"] forState:UIControlStateNormal];
                [btn addTarget:self action:@selector(Buttonclick:) forControlEvents:UIControlEventTouchUpInside];
                [ScrlPhotos addSubview:btn]; //Add the UIButton in UIScrollview
                [btn release];


                cnt++;
            }
        }
        [mainRestaurantArray release];
        counter++;
        [self urlcalled]; //Is the method which call the webservice do the parsing and fills the mainRestaurantArray as a responce
    }

The problem is, despite having added the abouve code, it takes much time to load, calls the web service like 10 times, and displays the images only then.

Can any one help me please?

ios developer
  • 3,363
  • 3
  • 51
  • 111

2 Answers2

4

The key is what's going on in [self urlCalled]. It does look like you're firing off 10 requests in that outer for-loop.

Are you using 5.0 SDK? If so, there's a nifty one-liner to make the web request and handle the result with a block. On 5.0, you can put this in your loop:

NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    if (data) {
        UIImage *image = [UIImage imageWithData:data];
        // use the image how you like, say, as your button background
    }
}];
danh
  • 62,181
  • 10
  • 95
  • 136
  • Thanks for the quick reply..u r right i m firingrequest 10times in for loop.And i want to execute my code in IOS 4 as well as ios 5 – ios developer Mar 17 '12 at 06:23
0

you can follow this link, even though it deals about lazy loading tableview you can apply the same concept to uiscrollview.

Community
  • 1
  • 1
Vignesh
  • 10,205
  • 2
  • 35
  • 73
  • @Vignest can u please explain it in how do i use Tableview's Lazyloading in UIscrollview.. – ios developer Mar 21 '12 at 05:43
  • 1
    @shweta. You can load your scrollview with dummy image first . load image from url asynchronously. Perhaps you can use GCD to get data. The advantage is your UI will not stuck. Here is an example http://dbrajkovic.wordpress.com/2012/01/08/load-images-asynchronously-in-a-uitableview-using-gcd-grand-central-dispatch/. You can consider using a tableview too. – Vignesh Mar 21 '12 at 07:37