ios - UIImageView 意外改变大小

标签 ios objective-c uitableview uiimageview

我有 UITableView ,其中的单元格上有图像。我使用 Storyboard,并将 UIImageView 连接为 socket 并将其命名为 imageView。我得到的图像大小不同(我通过 URL 下载)。但是,当表格显示时,有时图像显示的尺寸不正确(它应该是宽度和高度为 66x66 的正方形,但在许多情况下正方形更大,或者实际上宽度大于高度)。

显然我希望我的 UIImageView 的边框保持稳定。这是我的单元格代码:

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

    UILabel *labelText = (UILabel *)[cell viewWithTag:1000];
    labelText.text = [[self.listOfPlaceDetails objectAtIndex:indexPath.row] objectForKey:@"name"];

    if (cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    [cell.imageView setImageWithURL:[NSURL URLWithString:[[self.listOfPlaceDetails objectAtIndex:indexPath.row]objectForKey:@"imageFirst"]] placeholderImage:[UIImage imageNamed:@"dashie.png" ]completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType){

        UIImage *resizedImage = [image resizedImageWithBounds:CGSizeMake(66, 66)];
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];


        cell.imageView.layer.cornerRadius = cell.imageView.bounds.size.width /2.0f;
        cell.imageView.clipsToBounds = YES;
        cell.separatorInset = UIEdgeInsetsMake(0, 82, 0, 0);

       cell.imageView.image = resizedImage ;


    }];




    cell.imageView.contentMode = UIViewContentModeScaleAspectFit;

return cell;
}

请解释一下发生了什么以及如何解决这个问题,任何建议将不胜感激,谢谢!

最佳答案

添加新的imageView并设置图像。我建议您创建 uitablviewCell 的子类并将 imageView 添加为导出 View

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

        UILabel *labelText = (UILabel *)[cell viewWithTag:1000];
        labelText.text = [[self.listOfPlaceDetails objectAtIndex:indexPath.row] objectForKey:@"name"];

        if (cell == nil){
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:yourFrame];
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        [cell addSubview:imgView];
        imgView.tag = 7;
        }

       UIImageView *img = (UIImageView*)[cell viewWithTag:7];
       UIImage *resizedImage = [image resizedImageWithBounds:CGSizeMake(66, 66)];
       img.imqge = resizedImage

关于ios - UIImageView 意外改变大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21802334/

相关文章:

ios - 出现键盘时的 tableView 与 scrollView 行为

ios - 台风注入(inject) xib 加载表单元格

ios - 如何在不同设备的段控制中管理文本大小?

objective-c - Objective-C 中的非阻塞等待函数

ios - 通过 CoreData 属性显示 tableView 项目

ios - 将视频播放器添加到 UITableView Cell *Swift

ios - 如何停止 UIWebView 从 “bouncing” 垂直滚动到底部?

objective-c - 覆盖一个按钮的 [UIButton Appearance]

iPhone类图设计

ios - 将两个核心数据结果加入 1 个 NSDictionary