leena
leena

Reputation: 699

Loading images one by one

I want to load images one by one not all together.In the following code imagedata is the array containing urls from which i need to load images. Here is my code but no success.

-(void)loadSingleImage:(int)buttonTag

{

    UIButton *buttonImage =(UIButton *) [self.view viewWithTag:buttonTag];
    NSData *imagesubCategoryData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:[imageData objectAtIndex:buttonTag-30]]];
    [buttonImage setImage:[UIImage imageWithData:imagesubCategoryData] forState:UIControlStateNormal];
}
-(void)loadImageData

{

    for(int i=0;i<[imageData count];i++)
    {
        [self loadSingleImage:i+30];
        sleep(0.1);
    }

}

Upvotes: 4

Views: 258

Answers (3)

zaph
zaph

Reputation: 112873

Do not use sleep(), instead use a runloop if you must delay:

NSRunLoop* currentRunLoop = [NSRunLoop currentRunLoop];
for(int i=0;i<[imageData count];i++)
{
    [self loadSingleImage:i+30];
    NSDate* dateLimit = [NSDate dateWithTimeIntervalSinceNow:0.1];
    [currentRunLoop runUntilDate:dateLimit];
}

But a better solution is GCD.

Upvotes: 0

user523234
user523234

Reputation: 14834

You can use a NSTimer.. Let me known if you need sample code.

Upvotes: 1

MohanVydehi
MohanVydehi

Reputation: 356

You can use Grand Central Dispatch to load the images one by one..

Use the following code

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

dispatch_async(queue, ^{
    NSURL *url = [NSURL URLWithString: @"http://www.gev.com/wp-content/uploads/2011/05/two_flowers.preview.jpg"];

    dispatch_sync(dispatch_get_main_queue(), ^ {
        [cell.cellImage setImageWithURL: url placeholderImage: [UIImage imageNamed: @"twitter.jpg"]]; 
    });
});

It may helps you

Upvotes: 3

Related Questions