Upload images one at a time

I want to upload images one by one, not all together. In the following code, imagedata is an array containing the URLs from which I need to upload images. Here is my code, but not 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); } } 
+4
source share
3 answers

You can use NSTimer. Let me know if you need some sample code.

+1
source

You can use Grand Central Dispatch to download images one at a time.

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 can help you.

+3
source

Do not use sleep (), use runloop instead 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 the best solution is GCD.

0
source

All Articles