Skip to content

Instantly share code, notes, and snippets.

@ipreencekmr
Created August 3, 2013 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipreencekmr/6146286 to your computer and use it in GitHub Desktop.
Save ipreencekmr/6146286 to your computer and use it in GitHub Desktop.
#import "LazyLoad.h"
@implementation LazyLoad
-(id)init{
if (self==[super init]) {
UIImageView* imageView = [[UIImageView alloc] initWithFrame:self.frame];
imageView.backgroundColor=[UIColor blueColor];
[self addSubview:imageView];
}
return self;
}
- (void)loadImageFromURL:(NSURL*)url
{
NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData {
if (data==nil) { data = [[NSMutableData alloc] initWithCapacity:2048]; }
[data appendData:incrementalData];
}
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection
{
connection=nil;
if ([[self subviews] count]>0)
{
[[[self subviews] objectAtIndex:0] removeFromSuperview];
}
UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:data]] ;
// imageView.contentMode = UIViewContentModeScaleAspectFit;
// imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight );
[self addSubview:imageView];
imageView.frame = self.bounds;
[imageView setNeedsLayout];
[self setNeedsLayout];
data=nil;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment