zl程序教程

您现在的位置是:首页 >  其它

当前栏目

一个从UIImageView中获得Image位置的函数

函数 一个 位置 获得 Image
2023-09-14 09:00:29 时间
float hfactor = image.size.width / imageView.frame.size.width; float vfactor = image.size.he


 (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中找到的马克下!


Image图片查找不存在时(九) 在实际的开发中,常常会遇到关于图片的问题。 如图片显示的问题,然而在显示图片的过程中,常常也会遇到一些问题,如图片不存在,图片引用的路径不匹配等。
给一个 UIImageView 设置一张图片时,使用 AutoLayout 给 UIImageView 约束宽高,但是实际显示的大小,图片以实际的大小显示出来,代码也没有设置 frame,设置contentMode为UIViewContentModeScaleAspectFit 也不起作用。
响应式页面中经常用到根据屏幕密度设置不同的图片。这个时候肯定会用到image标签的srcset属性。srcset属性用于设置不同屏幕密度下,image自动加载不同的图片。