【问题标题】:Access the frame of a resized UIImage in a UIImageView在 UIImageView 中访问调整大小的 UIImage 的框架
【发布时间】:2011-09-01 14:10:04
【问题描述】:

我在 UIImageView 中使用 contentMode AspectToFit 设置了 UIImage。如何访问 调整大小的 UIImage 的框架?因为 imageView.image.size.width 给出了图像的原始宽度,而不是调整后的宽度。

谢谢,

【问题讨论】:

    标签: iphone ios resize uiimage


    【解决方案1】:

    UIImageView 的属性frame 将返回显示图像的框架。

    如果您想更改框架,您只需将新的CGRect frame 设置为您的UIImageView

    例如:

    UIImageView *imgView;
    imgView.frame = CGRectMake(0, 0, newWidth, newHeight);
    

    更新:

    要计算新尺寸,您可以使用以下代码:

    UIImage *img;
    UIImageView *iView;    
    double widthRatio = img.size.width/iView.frame.size.width;
    double heightRatio = img.size.height/iView.frame.size.height;
    double ratio = widthRatio>heightRatio?widthRatio:heightRatio;
    CGSize newImageSize;
    newImageSize.width = img.size.width/ratio;
    newImageSize.height = img.size.height/ratio;
    

    【讨论】:

    • 我知道,我正在寻找 newWidht 和 newHeight(调整大小的图像)
    • UIImageView.frame 会给你resized image的大小
    • 这就是我刚刚所做的。它证实了我的想法!
    猜你喜欢
    • 2011-09-16
    • 2018-09-23
    • 2013-01-16
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    • 2014-12-24
    • 1970-01-01
    • 2013-03-05
    相关资源
    最近更新 更多