(CGRect)getFrameSizeForImage:(UIImage *)image inImageView:(UIImageView *)imageView {
float hfactor = image.size.width / imageView.frame.size.width;
float vfactor = image.size.height / imageView.frame.size.height;
float factor = fmax(hfactor, vfactor);
// Divide the size by the greater of the vertical or horizontal shrinkage factor
float newWidth = image.size.width / factor;
float newHeight = image.size.height / factor;
// Then figure out if you need to offset it to center vertically or horizontally
float leftOffset = (imageView.frame.size.width - newWidth) / 2;
float topOffset = (imageView.frame.size.height - newHeight) / 2;
return CGRectMake(leftOffset, topOffset, newWidth, newHeight);
}废话不多说贴代码。再stackOverflow中找到的马克下!
本文提供了一段用于计算图片在UIImageView中等比例缩放并居中的 CGRect 的代码实现。该方法通过比较图片与视图的宽高比,确定缩放因子,并计算出新的宽度、高度及偏移量。
1555

被折叠的 条评论
为什么被折叠?



