【问题标题】:UIImageView Aspect to Fit not working when tableview scrolls当表格视图滚动时,UIImageView Aspect to Fit 不起作用
【发布时间】:2015-12-02 07:53:47
【问题描述】:

我使用下面的代码将图像缩放到我的 UIImageView。第一次加载表格时一切都很好。但是当我滚动 tableview 时,所有图像方面都被破坏了。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ProductCell" forIndexPath:indexPath];

    [self configureCustomCell:(ProductsTableViewCell*)cell atIndexPath:indexPath];

    return cell;
}

- (void)configureCustomCell:(ProductsTableViewCell*)cell atIndexPath:(NSIndexPath *)indexPath {

    NSURL *url = [NSURL URLWithString:[[arr_products objectAtIndex:indexPath.section] productImage]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    __weak UITableViewCell *weakCell = cell;

    [cell.productPhotoImage setImageWithURLRequest:request placeholderImage:nil
          success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                weakCell.imageView.image = image;
              weakCell.imageView.contentMode = UIViewContentModeScaleAspectFit;
              weakCell.imageView.clipsToBounds = YES;
              weakCell.imageView.frame = CGRectMake(5, 5, 60, 60);
          }
    failure:nil];
}

【问题讨论】:

  • 请在- tableView:cellForRowAtIndexPath:中显示您的代码。
  • @serdar aylnc 你在使用自定义单元格吗??
  • @satheesh 我更新了问题中的代码

标签: ios objective-c uitableview uiimageview


【解决方案1】:

如果你想从服务器获取图片然后加载到单元格tableviewcell上将无法正常升级,

您可以使用 SDWebImage 库从服务器加载图像,请查看此链接SDWEbImage

如果你想添加活动指示器,请查看https://github.com/JJSaccolo/UIActivityIndicator-for-SDWebImage

#import <SDWebImage/UIImageView+WebCache.h>

在 cellForRowAtIndexPath 中

[weakCell.myImage setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"mawgif-log.png"] options:SDWebImageRefreshCached usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

【讨论】:

    猜你喜欢
    • 2011-03-17
    • 2013-08-01
    • 2015-07-29
    • 2014-02-26
    • 1970-01-01
    • 2018-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多