在ios中延迟加载图像?

时间:2014-09-26 09:15:01

标签: ios objective-c image uitableview

我懒得在表格视图中加载一些图片

我按照下面的教程   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;
}

另外,在滚动表格视图时,它也会中断。我该如何解决这个问题?任何建议。

3 个答案:

答案 0 :(得分:1)

我建议您查看PHImageManager

swift中的示例代码

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
}

答案 1 :(得分: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];
}

答案 2 :(得分: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;

}