我正在尝试实现像facebook或Twitter时间轴这样的表格。但是当我滚动UITableView
时,它非常缓慢且滞后。我该如何解决这个问题?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ViewControllerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"htrcell"];
if (cell==nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSDictionary *dataArray = [self.threndsArray objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:[dataArray objectForKey:@"pic_timeline"]];
NSData *imgData = [NSData dataWithContentsOfURL:url];
cell.upload_image.image =[UIImage imageWithData:imgData];
NSURL *urlimage = [NSURL URLWithString:[dataArray objectForKey:@"pic_user"]];
NSData *Dataimage = [NSData dataWithContentsOfURL:urlimage];
cell.uploder_image.image =[UIImage imageWithData:Dataimage];
cell.uploder_image.clipsToBounds = YES;
cell.uploder_image.layer.cornerRadius = cell.uploder_image.frame.size.height /2;
cell.uploder_image.layer.borderWidth = 2.0;
cell.uploder_image.layer.borderColor = [UIColor whiteColor].CGColor;
cell.uploder_name.text=[dataArray valueForKeyPath:@"nama_user"];
cell.like_counter.text=[NSString stringWithFormat:@"%@",[dataArray valueForKeyPath:@"likes"]];
cell.commenter_name.text=[dataArray valueForKeyPath:@"status_timeline"];
cell.comments.text=[dataArray valueForKeyPath:@"tgl_timeline"];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
答案 0 :(得分:1)
主要问题是,您在主线程中将图片网址转换为/usr/bin/codesign
,这就是延迟滚动发生的原因。您需要将后台队列中的图像URL转换为NSData
并更新主队列中的UI。
这样你就可以在后台加载每个图像,加载后相应的单元格会在mainThread上更新。
以下代码可以帮助您
NSData
答案 1 :(得分:0)
您也可以尝试NSURLRequest
NSURL *url = [NSURL URLWithString:[dataArray objectForKey:@"pic_timeline"]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse * response,
NSData * data, NSError * error)
{
if (!error) {
cell.upload_image.image = [UIImage imageWithData:data];
}
}
答案 2 :(得分:0)
如果您使用某些URL
加载图片,那么最好的方法是加载图片Asynchronously
。因此,我建议您查看以下链接以加载图片Asynchronously
答案 3 :(得分:0)
由于图像尺寸大而发生这种情况。 1.在不降低后端质量的情况下,将图像尺寸减小到(低于200 kb) 2.在背景中下载图像
NSURLSessionDownloadTask *downloadPhotoTask = [[NSURLSession sharedSession]
downloadTaskWithURL:[NSURL URLWithString:@"Your_URL"] completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
UIImage *downloadedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:location]];
dispatch_async(dispatch_get_main_queue(), ^{
downloadAllImageview.image=downloadedImage;
[activityIndicator stopAnimating];
[activityIndicator removeFromSuperview];
});
}];
[downloadPhotoTask resume];
答案 4 :(得分:0)
最好的方法是SDWebImageView 下载并使用像贝娄一样:
导入.h
中的2个文件#import "SDWebImage/UIImageView+WebCache.h" #import "UIImageView+UIActivityIndicatorForSDWebImage.h"
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ViewControllerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"htrcell"];
if (cell==nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSDictionary *dataArray = [self.threndsArray objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:[dataArray objectForKey:@"pic_timeline"]];
[cell.upload_image setImageWithURL:[dataArray objectForKey:@"pic_timeline"] placeholderImage:nil usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
cell.uploder_image.clipsToBounds = YES;
cell.uploder_image.layer.cornerRadius = cell.uploder_image.frame.size.height /2;
cell.uploder_image.layer.borderWidth = 2.0;
cell.uploder_image.layer.borderColor = [UIColor whiteColor].CGColor;
cell.uploder_name.text=[dataArray valueForKeyPath:@"nama_user"];
cell.like_counter.text=[NSString stringWithFormat:@"%@",[dataArray valueForKeyPath:@"likes"]];
cell.commenter_name.text=[dataArray valueForKeyPath:@"status_timeline"];
cell.comments.text=[dataArray valueForKeyPath:@"tgl_timeline"];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
答案 5 :(得分:0)
Please Try SDWebimage Library or download image Asynchronous
If you using SDWebimage please try this method:-
[imageView sd_setImageWithURL:[NSURL URLWithString:@"https://graph.facebook.com/olivier.poitrey/picture"]
placeholderImage:[UIImage imageNamed:@"avatar-placeholder.png"]
options:SDWebImageRefreshCached];
答案 6 :(得分:0)
缓慢加载可能是因为您放置了大量数据对象。您可以使用一些缓存库或执行objective-C通过NSCache
类提供的缓存。您可以做的另一件事是在项目中使用Facebook提供的ComponentKit
库。
缓存库:
时间线图书馆: