0

当我为每个单元格设置图像时,我的应用程序滚动非常缓慢。我尝试过使用 SDImageWeb 项目,但它的滚动速度仍然很慢,并且单元格上的所有图像视图最终都会被拉伸。这是我在行单元格中的代码。

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

有任何想法吗?

4

2 回答 2

2

问题就在这里

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

每次滚动视图时,3 个单元格正在下载图像。在这里您正在调整不推荐的图像大小

您需要像这样在延迟加载中设置图像

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

一切都会被这个照顾。你可以通过导入这个SDImageCache类来获得它,它将在这个示例代码中可用在这里

于 2012-06-30T13:29:45.540 回答
0

您正在从远程源下载图像,因此每次加载新单元格时,图像都会被下载,这会降低您的应用程序速度,苹果有一个关于如何异步加载图像的示例项目

于 2012-06-30T13:22:54.043 回答