简体   繁体   English

如何避免使用UIImageView的contentmode UIViewContentModeScaleAspectFit保持高度恒定而损失图像质量?

[英]How to avoid losing image quality using UIImageView's contentmode UIViewContentModeScaleAspectFit keeping height constant?

I am using a collectionview inside a tableview for showing images which are being downloaded asynchronously. 我正在使用tableview内部的collectionview来显示异步下载的图像。 What I need to do is to show those images without squeezing. 我需要做的是显示这些图像而不会受到挤压。 I am keeping height of the collectionview constant, and calculating the images' sizes after they are downloaded. 我保持collectionview高度不变,并在下载图像后计算图像的大小。 I am keeping content mode of the UIIamgeView UIViewContentModeScaleAspectFit. 我保持UIIamgeView UIViewContentModeScaleAspectFit的内容模式。 This is what I have done to achieve my intended result. 这是我为实现预期结果所做的工作。

- (void)configureMediaCell:(MediaContainerViewCell *)weakCell withMediaItem:(MSZWallPostMedia *)mediaItem withDownloadImage:(UIImage *)image{
    if (mediaItem.isFrameSet == NO) {
        weakCell.isFrameSet = YES;
        weakCell.cellImageView.image = image;
        weakCell.cellImageView.contentMode = UIViewContentModeScaleAspectFit;
        CGRect frame = weakCell.cellImageView.frame;

        //Image Scaling
        CGSize scaleSize = [self imageScale:weakCell.cellImageView];

        scaleSize.width = scaleSize.width * image.size.width;
        scaleSize.height = scaleSize.height * image.size.height;
        frame.size.width = scaleSize.width;
        frame.size.height = scaleSize.height;
        weakCell.cellImageView.frame = frame;
        mediaItem.frame = frame;
        mediaItem.isFrameSet = YES;
        [weakCell.activityIndicatorView stopAnimating];
        [self.collectionView reloadItemsAtIndexPaths:@[weakCell.indexPath]];
    } else {
        [weakCell.activityIndicatorView stopAnimating];
        weakCell.cellImageView.image = image;
        weakCell.cellImageView.frame = mediaItem.frame;
        weakCell.cellImageView.contentMode = UIViewContentModeScaleAspectFit;
    }
}

For Image Scaling: 对于图像缩放:

- (CGSize)imageScale : (UIImageView *)imageView {
    CGFloat sx = imageView.frame.size.width / imageView.image.size.width;
    CGFloat sy = imageView.frame.size.height / imageView.image.size.height;
    CGFloat s = 1.0;
    switch (imageView.contentMode) {
        case UIViewContentModeScaleAspectFit:
            s = fminf(sx, sy);
            return CGSizeMake(s, s);
            break;

        case UIViewContentModeScaleAspectFill:
            s = fmaxf(sx, sy);
            return CGSizeMake(s, s);
            break;

        case UIViewContentModeScaleToFill:
            return CGSizeMake(sx, sy);

        default:
            return CGSizeMake(s, s);
    }
}

The problem in this approach is that I get the correct widths and heights but some images are shown smaller which is obvious because I am using Aspectfit property of imageview which is shown in the attached screen. 这种方法的问题是,我获得了正确的宽度和高度,但是显示的某些图像较小,这很明显,因为我使用的是所附屏幕上显示的imageview的Aspectfit属性。

图片

Any help regarding this would be appreciated. 关于此的任何帮助将不胜感激。 I need to keep images as they are keeping height of collectionviewitem fixed and variable width. 我需要保持图像不变,因为它们使collectionviewitem高度保持固定且宽度可变。

If I understand you correctly. 如果我正确理解您的意思。 You just need to find image width/height ratio and multiply desired height. 您只需要找到图像的宽高比,然后乘以所需的高度即可。

Example: 例:

Image 图片

width = 30, height = 10 宽度= 30,高度= 10


Cell 细胞

width = ?, height = 7 宽度=?,高度= 7

Ratio = 30 / 10 比例= 30/10

cell width = 7 * (30/10) = 21 像元宽度= 7 *(30/10)= 21

Cell size will become (21,7) 像元大小将变为(21,7)


Hope this is what you meant. 希望这就是你的意思。 But you will face the problem if the image is smaller than cell. 但是,如果图像小于单元格,您将面临问题。 Then it might become distorted. 然后它可能会失真。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有contentMode UIViewContentModeScaleAspectFit的UIImageView的UILabel - UILabel for UIImageView that has contentMode UIViewContentModeScaleAspectFit 在将contentMode设置为UIViewContentModeScaleAspectFit时,如何设置UIImageView左对齐或右对齐? - how to set the UIImageView align left or right when set the contentMode to UIViewContentModeScaleAspectFit? 使用UIViewContentModeScaleAspectFit在UIImageView中调整图像 - Fit image in UIImageView using UIViewContentModeScaleAspectFit 当contentMode UIViewContentModeScaleAspectFit时,禁用UIImageView调整大小的行为 - Disable UIImageView resizing behavior when contentMode UIViewContentModeScaleAspectFit UIImageview的图像的高度恒定 - UIImageview’s Image For Constant Height 如何使用 swift 使用 Animation 更改 UIImageView ContentMode? - How to Change UIImageView ContentMode With Animation using swift? 在运行时设置图像时不遵守UIImageView的contentmode - UIImageView's contentmode not respected when setting image during runtime Swift 3/4如何为UIImageView的contentMode制作动画 - Swift 3/4 How to animate contentMode of UIImageView UICollectionViewCell UIImageView 忽略 contentMode,溢出图像 - UICollectionViewCell UIImageView ignores contentMode, overflows image 如何为 UIImageView 提供 cornerRadius,contentMode 设置为 scaleAspectFit - How to provide a cornerRadius to a UIImageView with contentMode set to scaleAspectFit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM