Increasing performance of iPhone iPad application in asynchronous image downloading

in most of iOS application we need to download large number of images from server , for that we may use synchronous , but drawback in synchronous method  is , it blocks UI when data is huge,So to solve this problem we need to use asynchronous method ,here we use NSOperationqueue , and we have to create NSInvocationOperations to achieve this task , it enhances performance of application and never block UI.

NSOperationQueue *operationQueue = [[NSOperationQueue alloc]init];
int nTotImage = 5;
for(int i = 0; i  nTotImage ; i++)
NSInvocationOperation *invocationOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(DownloadAsynch:) object:[NSString stringWithFormat:@”%d”,i]];
[queue addOperation:invocationOperation];
[invocationOperation  release];

-(void)DownloadAsynch: (NSString*)imageNo
NSString *imgStr = [NSString stringWithFormat:@”yourURL/%@.jpg”,imageNo]; /// change to  your URL
NSData *imageData=[[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:imgStr]];
UIImage *image=[[UIImage alloc]initWithData:imageData];
[imgDict setValue:image forKey:@”image”];
[imgDict setValue:imageNo forKey:@”imageNo”];
[self performSelectorOnMainThread:@selector(DisplayImg:) withObject:imgDict waitUntilDone:NO];

-(void)DisplayImg:(NSMutableDictionary *)Dict
int i = [[Dict objectForKey:@”imageNo”] intValue];
// waana display all images in single image view
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
[imgView setImage:[Dict objectForKey:@”image”]];
[self.view addSubview:imgView];

// wanna display all downloaded images in scrollview

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(320*i, 0, 320, 320)];
[imgView setImage:[Dict objectForKey:@”image”]];
[YourScrolllView addSubview:imgView];

Leave a Reply

Your email address will not be published. Required fields are marked *