UIImage 未设置为 AspectFill - iOS

UIImage not setting as AspectFill - iOS

我正在使用可扩展的 TableView,(HVTableView),我在其中设置了一些图像,这些图像是从 URL 下载的。问题是图像没有设置为 AspectFill,即使我已经在 Storyboard 和代码中设置了所有内容。

故事板:

代码:

UIImageView *imageView = (UIImageView *)[cell viewWithTag:0];
if (object.imageFile) {
            
            
            [object.imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
                if (!error) {
                    UIImage *image = [UIImage imageWithData:data];
                    [imageView setClipsToBounds:YES];
                    [imageView setContentMode:UIViewContentModeScaleAspectFill];
                    [imageView setImage:image];
                }
                else [imageView setImage:[UIImage imageNamed:@"placeholder"]]; //initiL state
            }];
            
        }
        
        else {
            [imageView setImage:[UIImage imageNamed:@"placeholder"]];
        }

结果:

如果我使用本地图像,那么它们会完美显示,如 square 和 aspectFill 或任何设置的 contentMode

根据以下评论更新

你介意试试这个吗?

UIImage *image = [UIImage imageWithData:data];
image = [UIImage imageWithCGImage:image.CGImage];

问题不在于 UIImageView,而在于 Table View 正在根据图像的大小计算单元格的高度。

尝试覆盖 table 视图控制器中的 tableView:heightForRowAtIndexPath: 和 return 一个固定值。

好吧,我只是胡乱猜测并尝试子类化 UITableViewCell(将 UIImageViewUILabel 设为 IBOutlets),现在成功了!这些图像完美地表现为 aspectFill.

我想获取单元格的内容然后设置项目(如问题中)不是一个聪明的方法。对解决方案进行子类化!

感谢大家的帮助。