2014-09-26 123 views
0

我懒加载在表视图懒惰加载图像在iOS?

一些图片我也跟着下面的教程 Lazy Load ios

这就是我想要显示的单元格样式。 Table Cell View

问题是在服务器端图像太大,因此图像视图需要图像的全部宽度和高度。这会导致表格的突然显示。

我无法缩小服务器的大小。没有任何方法可以为所有正在加载的图像设置图像的高度和宽度。

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

CellTableViewCell *cell =(CellTableViewCell *)[tableView1 dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

if (cell == nil) { 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"videoCell" owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 
} 

cell.videoNameLabel.text = [videoTitle objectAtIndex:indexPath.row]; 


cell.videoImage.contentMode = UIViewContentModeScaleAspectFill; 

[cell.videoImage sd_setImageWithURL:[NSURL URLWithString:[videoImage objectAtIndex:indexPath.row]] 
       placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 


return cell; 
} 

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
return 200; 
} 

再加上滚动表视图它也会中断。我如何解决这个问题?有什么建议么。

+0

建议看着PHImageManager

示例代码中你_diskCachePath保存图像之前,调整图像的大小,然后保存。 当您滚动表格视图时,它将加载较小尺寸的图像,并且看起来平滑滚动。 – 2014-09-26 09:45:34

回答

0

单元重复使用未正确实施。

添加以下代码:

- (void)viewDidLoad 
{ 
    [super viewDidload]; 
    [self.tableView registerNib:[UINib nibWithNibName:@"videoCell" bundle:nil] 
     forCellReuseIdentifier:@"videoCell1"]; 
} 

,并删除该代码

if (cell == nil) { 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"videoCell" owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 
} 
+0

获取此错误'NSInternalInconsistencyException',原因:'nib(cell1)中的单元重用标识符与用于注册nib(videoCell1)'的标识符不匹配' – WISHY 2014-09-26 09:38:38

+0

您已为cell1定义了单元重用标识符(在界面构建器中打开并更改到“videoCell1”),或将代码更改为“cell1” – l0gg3r 2014-09-26 09:40:28

0

在_diskCachePath保存图像之前,调整图像的大小,然后保存。当您滚动表格视图时,它将加载较小尺寸的图像,并且看起来平滑滚动。

要调整图像大小,请使用以下方法。这将在调整图像大小时保持宽高比。

-(UIImage*) imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize{ 

CGFloat width = image.size.width; 
CGFloat height = image.size.height; 

if ((width/newSize.width) > (height/newSize.height)) { 

    if (width < newSize.width) { 
     newSize.width = width; 
    } 
    newSize.height = height*(newSize.width/width); 
} 
else if ((width/newSize.width) < (height/newSize.height)) { 

    if (height < newSize.height) { 
     newSize.height = height; 
    } 
    newSize.width = width*(newSize.height/height); 
} 
else if(newSize.width > width && newSize.height > height){ 

    newSize.height = height; 
    newSize.width = width; 

} 
UIGraphicsBeginImageContext(newSize); 
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; 
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

return newImage; 

}

1

我会迅速

var assets: [PHAsset] 
var imageRequests: [NSIndexPath: PHImageRequestID] 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 

    let manager = PHImageManager.defaultManager() 

    if let request = imageRequests[indexPath] { 
     manager.cancelImageRequest(request) 
    } 

    let asset = assets[indexPath.row] 

    cell.textLabel?.text = NSDateFormatter.localizedStringFromDate(asset.creationDate, dateStyle: .MediumStyle, timeStyle: .MediumStyle) 

    imageRequests[indexPath] = manager.requestImageForAsset(asset, targetSize: CGSize(width: 100.0, height: 100.0), contentMode: .AspectFill, options: nil) { (result, _) in 
     cell.imageView?.image = result 
    } 

    return cell 
} 
+0

这几乎为我工作,不得不改变一些Swift 3的东西。'var imageRequests:[IndexPath:PHImageRequestID]'等等:) – Friesgaard 2016-12-07 08:18:22