[英]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.