ios中的延迟加载图像?

ios中的延迟加载图像?,ios,objective-c,image,uitableview,Ios,Objective C,Image,Uitableview,我懒得在表视图中加载一些图像 我遵循了下面的教程 这是我要显示的单元格样式。 问题在于服务器端映像太大,因此映像视图会占用映像的全宽和全高。这会导致表格的突然显示 我不能减少服务器上的大小。没有任何方法可以为所有正在延迟加载的图像设置图像高度和宽度 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSStr

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

我遵循了下面的教程

这是我要显示的单元格样式。

问题在于服务器端映像太大,因此映像视图会占用映像的全宽和全高。这会导致表格的突然显示

我不能减少服务器上的大小。没有任何方法可以为所有正在延迟加载的图像设置图像高度和宽度

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

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

未正确执行单元重用

添加此代码:

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

在将图像保存到_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;
-(UIImage*)图像带图像:(UIImage*)图像缩放大小:(CGSize)新闻大小{
CGFloat width=image.size.width;
CGFloat高度=image.size.height;
如果((宽度/新闻大小.宽度)>(高度/新闻大小.高度)){
如果(宽度<新闻大小宽度){
newSize.width=宽度;
}
newSize.height=高度*(newSize.width/width);
}
else if((宽度/新闻大小.宽度)<(高度/新闻大小.高度)){
如果(高度<新闻大小高度){
newSize.height=高度;
}
newSize.width=宽度*(newSize.height/height);
}
else if(newSize.width>width&&newSize.height>height){
newSize.height=高度;
newSize.width=宽度;
}
UIGraphicsBeginImageContext(新闻大小);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage*newImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
返回新图像;

}我建议你看看

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
}

获取此错误“NSInternalInconsistencyException”,原因:“nib中的单元重用标识符(cell1)与用于注册nib的标识符(videoCell1)不匹配”您已将单元重用标识符定义为cell1(在interface builder中打开它并更改为“videoCell1”),或将代码更改为“cell1”在将图像保存到_diskCachePath中之前,请调整图像大小,然后保存。当你滚动表格视图时,它将加载较小尺寸的图像,并且看起来滚动很平滑。这几乎对我有效,我不得不为Swift 3更改一些内容<代码>var imageRequests:[IndexPath:PHImageRequestID]等:)