Sunday 25 December 2011

Iphone UIScrollView Lazy Image Loading.

In Your *.H File put this code

@interface ScrollviewLazyLoadingViewController : UIViewController
<UIScrollViewDelegate, thumbImageViewDelegate>
{
    IBOutlet UIScrollView *Scorll_View;
    BOOL isDragging_msg,isDecliring_msg;
    NSMutableDictionary *dicImages_msg;
    NSMutableArray *ImgesName;  
}
@property(nonatomic,retain) NSDictionary *results;
@property(nonatomic,retain) NSMutableArray *ImgesName;
-(void)DownLoad:(NSNumber *)path;
-(void) reloadScrolview;
@end

Now Add this code in your  class.m File

- (void)viewDidLoad
{
    [super viewDidLoad];
    [Scorll_View setContentSize:CGSizeMake(300, 1200)];
    Scorll_View.delegate = self;
    dicImages_msg = [[NSMutableDictionary alloc] init];
    NSURLRequest *Projrequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"Your Image Store Link"]];
    NSData *response = [NSURLConnection
                        sendSynchronousRequest:Projrequest
                        returningResponse:nil error:nil];
    NSString *json_string = [[NSString alloc]
                             initWithData:response encoding:NSUTF8StringEncoding];
    SBJSON *jsonParser = [SBJSON new];
   self.results = [jsonParser objectWithString:json_string error:nil];
    NSLog(@"Dictionary : %@", results);
    self.ImgesName = [results valueForKey:@"images"];
    [self reloadScrolview];
}

-(void) reloadScrolview
{
    int x=10;
    int y=0;
    for(int i=0; i<[self.ImgesName count];)
    {
        for (int k=0; k<2; k++) 
        {
            if(i< [self.ImgesName count])
            {
                thumbImageView *imgView = [[thumbImageView alloc] initWithFrame:CGRectMake(x, y, 75,75)];
                if([dicImages_msg valueForKey:[self.ImgesName objectAtIndex:i]])
                {
                    imgView.image = [dicImages_msg valueForKey:[self.ImgesName objectAtIndex:i]];
                }
                else
                {
                    if(!isDragging_msg && !isDecliring_msg)
                    {
                       [dicImages_msg setObject:[UIImage imageNamed:@"Placeholder.png"] forKey:[self.ImgesName objectAtIndex:i]];
                        [self performSelectorInBackground:@selector(DownLoad:) withObject:[NSNumber numberWithInt:i]];
                    }
                    else
                    {
                        imgView.image = [UIImage imageNamed:@"Placeholder.png"];
                    }
                }
                imgView.userInteractionEnabled = YES;
                [imgView setDelegate:self];
                imgView.tag = i;
                imgView.exclusiveTouch = YES;
                imgView.clipsToBounds = NO;
                imgView.opaque = YES;
                [imgView.layer setBorderWidth: 2.0];
                [Scorll_View addSubview:imgView];
                x+=120;
                i++;
            }
        }
        x=10;
        y+=80;
        
    }

}
-(void)DownLoad:(NSNumber *)path
{
NSAutoreleasePool *pl = [[NSAutoreleasePool alloc] init];
    int index = [path intValue];
NSString *Link = [NSString stringWithFormat:@"%@%@",[results valueForKey:@"imageurl_middle"],[self.ImgesName objectAtIndex:index]];
UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:Link]]]; 
    if(img)
    {
        [dicImages_msg setObject:img forKey:[self.ImgesName objectAtIndex:index]];
    }
[self performSelectorOnMainThread:@selector(reloadScrolview) withObject:path waitUntilDone:NO];
[pl release];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
isDragging_msg = FALSE;
    [self reloadScrolview];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
isDecliring_msg = FALSE;
[self reloadScrolview];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
isDragging_msg = TRUE;
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
isDecliring_msg = TRUE;
}


8 comments:

  1. Not working for me
    Can u share sample code

    ReplyDelete
  2. i have already share source code, you just need to modify it as per your requirement.

    ReplyDelete
  3. yes this is working for me now but i pickup first time code from here
    http://zubairraihan.blogspot.in/2012/02/lazy-loading-in-uiscrollview-in.html which has problem ,displaying only one image because of loop.
    Any way thanks

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. the loop does not stop until all images downloaded and placed in the scrollview!

    ReplyDelete
  6. if possible then give me example for images from URL... post inside UItableviewCell

    ReplyDelete
  7. Hi, how do i see the source code you shared? Need to implement something in this line, and your code will be most helpful. Thanks for sharing Rizwan :)

    ReplyDelete