0

Possible Duplicate:
Lazy load images in UITableView

I have tableview with 40 rows, and i want to add images to each cell from URL. But the problem is that when I'm launching my app, it freezes for some time until all images are loaded from various URLs? I want images to be displayed one by one as they are loaded, and my app must not freeze. So pls answer me...?

Community
  • 1
  • 1

1 Answers1

0
@interface ImageLoading : UIView {

NSURLConnection* connection;
NSMutableData* data;
NSArray *userArr1;
UIActivityIndicatorView *progress;
}

 @property (nonatomic, retain) NSArray *userArr1;

  - (void)loadImageFromURL:(NSURL*)url;

  @end

And then in the implementation file

  @implementation ImageLoading

   - (id)initWithFrame:(CGRect)frame
  {
self = [super initWithFrame:frame];
if (self) {
    // Initialization code
    }
 return self;
   }

   - (void)loadImageFromURL:(NSURL*)url {    

   progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(5, 5, 25, 25)];
progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
[self addSubview:progress];
[progress startAnimating];

NSURLRequest* request =[NSURLRequest requestWithURL:url];

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.frame = self.bounds;
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight );    

// Begin a new image that will be the new image with the rounded corners 
// (here with the size of an UIImageView)
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);

// Add a clip before drawing anything, in the shape of an rounded rect
[[UIBezierPath bezierPathWithRoundedRect:imageView.bounds 
                            cornerRadius:5.0] addClip];
// Draw your image
[[UIImage imageWithData:data] drawInRect:imageView.bounds];

// Get the image, here setting the UIImageView image
imageView.image = UIGraphicsGetImageFromCurrentImageContext();

// Lets forget about that we were drawing
UIGraphicsEndImageContext();

[self addSubview:imageView];
[progress stopAnimating];

[imageView setNeedsLayout];
[self setNeedsLayout];
data=nil;
  }

  - (UIImage*) image {

 UIImageView* iv = [[self subviews] objectAtIndex:0];
 return [iv image];
   }

 @end

In your table view cell

   ImageLoading* asyncImage = [[ImageLoading alloc]
                               initWithFrame:frame];
    asyncImage.tag = 999;
    [asyncImage loadImageFromURL:url];
  [cell.contentView addSubview:asyncImage];

you can use this

same question

Community
  • 1
  • 1
Ramz
  • 596
  • 1
  • 6
  • 23
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – The iOSDev Nov 20 '12 at 07:20
  • Now I edited with essential code – Ramz Nov 20 '12 at 07:40
  • Rather than just copy the code over from the other answer, it would have been better if you just marked the question as a duplicate and linked to the other question. – Abizern Nov 20 '12 at 10:58