Loading images in background (Lazy Loading).

L

Some times we need to take images from server and on clicking performing some actions.
This code help you how to get one by one images and displaying into the app.

Step 1: Decalre NSMutablearray *ImgPathArr in .h file.

in .m File add below all methods

Step 2: Call below method, use your web service and add all ImagePath into ImgPathArr.

-(void)LoaMoreBooksAppImg
{
ImgPathArr = [[NSMutableArray alloc] init];

NSString *login_chk = [NSString stringWithFormat:@”http://www.Domain.com/Applications/xyz/ImagePath.xml”];
login_chk = [login_chk stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:login_chk];
NSData *loginData = [NSData dataWithContentsOfURL:url];

if([loginData length] != 0)
{
NSString *xmlResponse = [[NSString alloc] initWithData:loginData encoding:NSASCIIStringEncoding];
if(xmlResponse == nil)
xmlResponse = [NSString stringWithUTF8String:[loginData bytes]];

NSString *results = [self exctractStringFromTags:xmlResponse :@”” :@””];

NSArray *recordsArr = [results componentsSeparatedByString:@””];
for(int i = 0; i < [recordsArr count]-1; i ++)
{
NSString *imgpath = [self exctractStringFromTags:[recordsArr objectAtIndex:i]:@"” :@””];

[ImgPathArr addObject:imgpath];

}

for (int m = 0; m < ImgPathArr.count; m++)
{
// [ArrImage addObject:[UIImage imageNamed:@"TransparentImg.png"]];
UIImage *Img = [UIImage imageNamed:@"TransperentNew1.png"];
[ArrImage addObject:Img];

}

//Add here your Button positions here
int xpos = 100;//22;
int ypos = 120;//20;
int cnt = 1;
for(int i = 0; i 4)
{
cnt = 1;
ypos += 310;//95;
xpos = 100;//22;
}

UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.tag = i;
[btn setFrame:CGRectMake(xpos,ypos,201, 281)];
//NSLog(@”%d, %d”, xpos, ypos);

[btn setBackgroundImage:[ArrImage objectAtIndex:i] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(ButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[MoreBooks_ScrollView addSubview:btn];

xpos += 210;//95;
cnt++;

}

//Added End

MoreBooks_ScrollView.contentSize = CGSizeMake(1024, ImgPathArr.count * 100);
}

[GlobalView addSubview:MoreBooks_ScrollView];

[self loadImagesFromURL];
}

// Extacting ImagePath from tag
– (NSString *) exctractStringFromTags : (NSString *) str : (NSString *) startTag : (NSString *) endTag
{
NSScanner *theScanner;
NSString *text = nil;

theScanner = [NSScanner scannerWithString:str];

while ([theScanner isAtEnd] == NO) {

// find start of tag
[theScanner scanUpToString:startTag intoString:NULL] ;

// find end of tag
[theScanner scanUpToString:endTag intoString:&text] ;

// replace the found tag with a space
//(you can filter multi-spaces out later if you wish)

text = [text stringByReplacingOccurrencesOfString:[NSString stringWithFormat:startTag] withString:@””];
}

return text;
}

Step 2: Calling loadImagesFromURL method

-(void) loadImagesFromURL
{
// Create a pool
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

for(int i = 0; i = 1)
[self performSelectorOnMainThread:@selector(assignImageToImageView:) withObject:arr waitUntilDone:YES];
else
NSLog(@”Array null”);
}

– (void) assignImageToImageView:(NSArray *)imgAndTagReference
{
// Create a pool
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

//NSLog(@”Assigning image to index : %d”, [[imgAndTagReference objectAtIndex:1] intValue]);

if([[imgAndTagReference objectAtIndex:1] intValue] < [ArrImage count] )
{
[ArrImage replaceObjectAtIndex:[[imgAndTagReference objectAtIndex:1] intValue] withObject:[imgAndTagReference objectAtIndex:0]];

UIButton *userButton;
NSArray *subviews = [MoreBooks_ScrollView subviews];

for(int i = 0; i < [subviews count]; i++)
{
UIView *view = [subviews objectAtIndex:i];
if([view isKindOfClass:[UIButton class]] == TRUE)
{
userButton = (UIButton *) view;

int index;
for(int j = 0; j < [ArrImage count]; j++)
{
if(userButton.tag == j)
{
index = j;
break;
}

}
[userButton setImage:[ArrImage objectAtIndex:index] forState:UIControlStateNormal];
//break;
}

}
}
// release the pool
[pool release];

}

About the author

mayur.bhansali
By mayur.bhansali

Category