user717452
user717452

Reputation: 111

iPhone UITableView Scrolling Slow With Cell ImageView

My app is scrolling very slow when I have Images set for each cell. I have tried using the SDImageWeb Project, but it still scrolls just as slow, and all the image views on the cell end up getting stretched around. Here is my code in the cell at row.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];


}



RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];

//root path of image...used to check if image exists for this article
NSString *substring = @"http://316apps.com/ipreachersblog/wp";
NSRange textRange = [entry.articleImage rangeOfString:substring];

if(textRange.location != NSNotFound){
    NSString *thearticleImage = entry.articleImage;
    NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:@"(?i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))" options:NSRegularExpressionCaseInsensitive error:NULL];
    NSString *someString = thearticleImage;
    NSString *oneurl = [someString substringWithRange:[expression rangeOfFirstMatchInString:someString options:NSMatchingCompleted range:NSMakeRange(0, [someString length])]];
    NSURL *picimage = [NSURL URLWithString:oneurl];
    UIFont *cellFont = [UIFont fontWithName:@"ArialRoundedMTBold" size:15];

    UIFont *cellFont2 = [UIFont fontWithName:@"ArialRoundedMTBold" size:12];    
    NSData * urlData = [NSData dataWithContentsOfURL: picimage];
    UIImage * imageweb = [UIImage imageWithData: urlData];
    CGSize newSize = CGSizeMake(69, 69);
    UIGraphicsBeginImageContext( newSize );
    [imageweb drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CALayer * l = [cell.imageView layer];
    [l setMasksToBounds:YES];
    [l setCornerRadius:11];
    [l setBorderWidth:2.0];
    [l setBorderColor:[[UIColor blackColor] CGColor]];
    cell.textLabel.text = entry.articleTitle; 

    cell.textLabel.font = cellFont;
    cell.detailTextLabel.font = cellFont2;
    cell.imageView.image = newImage;
    cell.imageView.contentMode = UIViewContentModeScaleAspectFill;


}
else    {
 //loads when no featured image present

}


return cell;
}

Any ideas?

Upvotes: 0

Views: 1425

Answers (2)

Sumanth
Sumanth

Reputation: 4921

The problem is here

NSURL *picimage = [NSURL URLWithString:oneurl];
UIFont *cellFont = [UIFont fontWithName:@"ArialRoundedMTBold" size:15];

UIFont *cellFont2 = [UIFont fontWithName:@"ArialRoundedMTBold" size:12];    
NSData * urlData = [NSData dataWithContentsOfURL: picimage];
UIImage * imageweb = [UIImage imageWithData: urlData];
CGSize newSize = CGSizeMake(69, 69);
UIGraphicsBeginImageContext( newSize );
[imageweb drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CALayer * l = [cell.imageView layer];
[l setMasksToBounds:YES];
[l setCornerRadius:11];
[l setBorderWidth:2.0];
[l setBorderColor:[[UIColor blackColor] CGColor]];

For every time you scroll the view 3 cells are downloading the images. Here you are resizing images which is not recommended

You need to set image in Lazy load like this

[cell.imageView setImageWithURL:picimage placeholderImage:[UIImage imageNamed:@"loadingPicture.png"]];

and every thing will be taken care by this. you can get this by importing this SDImageCache class, it will be available in this sample code at here

Upvotes: 2

Omar Abdelhafith
Omar Abdelhafith

Reputation: 21221

You are downloading the image from a remote source, so each time you load a new cell, the image gets downloaded and that will slow your application down, apple has a sample project on how to load the image asynchronously

Upvotes: 0

Related Questions