Search code examples
iosobjective-cnsurlconnectionlazy-loading

How to update UIImages after NSURLConnection delegate completes


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];
    }
}

Solution

  • 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;
    }