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 n开发者_运维百科o 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);
}
}
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
You can use a NSTimer.. Let me known if you need sample code.
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.
精彩评论