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

简介: (CGRect)getFrameSizeForImage:(UIImage *)image inImageView:(UIImageView *)imageView { 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中找到的马克下!

目录
相关文章
|
存储 Swift
Swift - Cell自适应+代码约束(SnapKit)横竖屏支持平铺+根据URL获取图片size
Swift - Cell自适应+代码约束(SnapKit)横竖屏支持平铺+根据URL获取图片size
179 0
UITextView根据NSString计算Size
UITextView根据NSString计算Size
42 0
VC中GDI+双缓冲实现Picture控件中显示png图片
VC中GDI+双缓冲实现Picture控件中显示png图片
166 0
UIImageView 图片自适应大小
UIImageView 图片自适应大小
106 0
|
存储 JavaScript 前端开发
Image图片查找不存在时(九)
在实际的开发中,常常会遇到关于图片的问题。 如图片显示的问题,然而在显示图片的过程中,常常也会遇到一些问题,如图片不存在,图片引用的路径不匹配等。
256 0
|
Python
Halcon读取dxf文件生成xld,然后实现点坐标遍历/缩放/镜像/求最大面积等操作(★firecat推荐★)
Halcon读取dxf文件生成xld,然后实现点坐标遍历/缩放/镜像/求最大面积等操作(★firecat推荐★)
558 0
VC2005中将Picture控件显示图片保存为BMP,JPG等格式
1.在stdafx.h头文件中加入   #include 2.保存图片   方法一:     HBITMAP hBitmap = NULL; //创建位图段 BITMAPINFO bmi; LPBYTE pBits; ZeroMemory(&bmi,sizeof(bmi)); //m...
973 0