当我为每个单元格设置图像时,我的应用程序滚动速度非常慢。我尝试过使用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;
}
有什么想法吗?
答案 0 :(得分: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个单元格都在下载图像。在这里您要调整不推荐的图像
您需要像这样
在Lazy load中设置图像[cell.imageView setImageWithURL:picimage placeholderImage:[UIImage imageNamed:@"loadingPicture.png"]];
并且每件事都会被照顾。您可以通过导入此SDImageCache
课程来获取此内容,此示例代码位于here
答案 1 :(得分:0)
您正在从远程源下载图像,因此每次加载新单元格时,图像都会下载,这会降低您的应用程序速度,Apple在how to load the image asynchronously上有一个示例项目