0

So I'm pulling down about 50 images from my API using NSURLConnection, its working great, except its locking up the UI when it runs. I'm assuming that is because I'm updating the UI in real time form the NSURLConnection self delegate. So I'm thinking what I need to do is put placeholder loading images in the UIImage, then update them somehow once the delegate has acquired all the data, but how do I do that, can someone give me some coding examples?

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    // The request is complete and data has been received
    // You can parse the stuff in your instance variable now

    NSData *imageData = _dataDictionary[ [connection description] ];

    if(imageData!=nil)
    {
    NSLog(@"%@%@",[connection description],imageData);

    UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(self.x, 0, self.screenWidth, self.screenHight)];

    // Process thi image
    // resize the resulting image for this device
    UIImage *resizedImage = [self imageScaleCropToSize:[UIImage imageWithData: imageData ]];

    self.x = (self.x + imageView.frame.size.width);
    if(self.x > self.view.frame.size.width) {
        self.scrollView.contentSize = CGSizeMake(self.x, self.scrollView.frame.size.height);
    }

    [imageView setImage:resizedImage];
    // add the image
    [self.scrollView addSubview: imageView];
    }
}
LeviXC
  • 1,075
  • 2
  • 15
  • 32

2 Answers2

2

You can use SDWebImage library to achieve this.

Suppose imageArray have all the image url path. You can use SDWebImageManager to download all the images and show them in ImageView. Also you can show downloading progress using this block.

- (void)showImages:(NSArray *)imageArray
{
  SDWebImageManager *manager = [SDWebImageManager sharedManager];
  for (NSString *imagePath in imageArray)
  {
      [manager downloadImageWithURL:[NSURL URLWithString:imagePath]
                          options:SDWebImageLowPriority
                         progress:^(NSInteger receivedSize, NSInteger expectedSize){}
                        completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL)
         {

           if(!error)
               self.imgView_Image.image = image;
           else
            {
               UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"please check your  Connection and try again"  message:@"No Internet Connection" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles: nil];
               [alert show];
            }
         }];
  }
}
nilam_mande
  • 931
  • 7
  • 14
1

First create protocol in that class .h, where you call NSURLConnection request for download image (Where you implement this method connectionDidFinishLoading).

@protocol YourClassNameDelegate <NSObject>

- (void)didFinishLoadingImage:(UIImage *)downloadImage;

@end

and create property for that protocol in same class,

@interface YourViewController : UIViewController

@property (nonatomic, retain) id<YourClassNameDelegate>delegate;

@end

then synthesise it in .m, @synthesize delegate;

After that call didFinishLoadingImage: in connectionDidFinishLoading,

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
// You can parse the stuff in your instance variable now

NSData *imageData = _dataDictionary[ [connection description] ];

if(imageData!=nil)
{
NSLog(@"%@%@",[connection description],imageData);

UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(self.x, 0, self.screenWidth, self.screenHight)];

// Process thi image
// resize the resulting image for this device
UIImage *resizedImage = [self imageScaleCropToSize:[UIImage imageWithData: imageData ]];

self.x = (self.x + imageView.frame.size.width);
if(self.x > self.view.frame.size.width) {
    self.scrollView.contentSize = CGSizeMake(self.x, self.scrollView.frame.size.height);
}

[self.delegate didFinishLoadingImage:resizedImage];
[imageView setImage:resizedImage];
// add the image
[self.scrollView addSubview: imageView];
}
}

and finally from where you push to YourViewController set delegate to self, like :

YourViewController *controller = [[YourViewController alloc] init];
controller.delegate = self;
//.....

in YourViewController.m, where you want to set downloaded image, in that class implement this method.

#pragma mark - YourClassName delegate method

- (void)didFinishLoadingImage:(UIImage *)downloadImage
{
    //yourImageView.image = downloadImage;
}
VRAwesome
  • 4,721
  • 5
  • 27
  • 52