2

I have an array of URL of images, and i want to display the image of each URL on a separate cell of the UITableView. In my previous code i used:

    UIImageView *view_Image = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, 400, 180)];

    view_Image.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:arrURL[i]]]];

This was inside a for loop such that the values of x,y and i would change for every UIImageView. However this approach does not work when i use this in cellForRowAtIndexPath. The images are not getting displayed, my implementation for cellForRowAtIndexPath is as:

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *simpleIdentifier = @"simpleIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentifier];
if(cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleIdentifier];
}

cell.textLabel.text = _arrNames[indexPath.row];

UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 300, 150)];
imv.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:arr[i]]]];

[cell.contentView addSubview:imv];
return cell;
}

Also when i directly add the URL of the image, the image is displayed in every row but the scrolling lags a lot. Any help will be appreciated. Thanks.

3

7 Answers 7

2

use sdwebimage library sdwebImage

Sign up to request clarification or add additional context in comments.

Comments

1

USE CODE BELOW TO DOWNLOAD IMAGE IN BACKGROUND AND SET IT TO CELL'S IMAGEVIEW.

UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 300, 150)];
[imv sd_setImageWithURL:[NSURL URLWithString:arrURL[i]] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]];
[cell.contentView addSubview:imv];

I hope this will help you.

Comments

0

Use this library, it will help you.

https://github.com/nicklockwood/AsyncImageView

it scrolls uitableview smoothly.

Comments

0

in cellForRowAtIndexPath, check for the availability of the image in the cache(first time it wont there anyway), assign it if present and start download if not present.

Once the download is complete, save the image in the cache and reload the tabeview, which inturn will call the cellForRowAtIndexPath, but this time image will be available in the cache.

I haven't covered all the edge cases like handling download failure, which I hope you will figure out.

Comments

0
UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 300, 150)];
[imv sd_setImageWithURL:[NSURL URLWithString:arrURL[i]] placeholderImage:    [UIImage imageNamed:@"placeholder.jpg"]];
[cell.contentView addSubview:imv];

Comments

0

You can use AFNetworking Library.

Import this header in your view controller

#import "UIImageView+AFNetworking.h"

Use below code to download image:

[imv setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:model.imageURL]] placeholderImage:[UIImage imageNamed:@"placeholder_gallery.png"];

Comments

0

You can download the library SDWebImage and then import it into your project.

#import "UIImageView+UIActivityIndicatorForSDWebImage.h"

and then write following code in your UITableView delegate method.

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *simpleIdentifier = @"simpleIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentifier];
if(cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleIdentifier];
}

cell.textLabel.text = _arrNames[indexPath.row];
    NSURL *imgURL = [NSURL URLWithString:@"Your URL String"];

    [cell.imgView sd_setImageWithURL:imgURL placeholderImage:[UIImage imageNamed:@"no-image"] options:SDWebImageRefreshCached completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (image) {
            cell.imv.image = image;
        }else{
            cell.imv.image = [UIImage imageNamed:@"no-image"];
        }
    }];

[cell.contentView addSubview:imv];
return cell;
}

May be this is helpful to you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.