如何从UIImageView获取显示的图像帧?

我修改了UIImageView的contentMode属性。 所以我的图像的框架不等于UIImageView的框架。 我的问题是如何从UIImageView中检索图像的框架。

如下例所示,橙色矩形是UIImageView的边框。 显然,图像的框架与UIImageView不同。

图1


如果它是UIViewContentModeScaleAspectFit ,它就像这样:

UIImageView *iv; // your image view
CGSize imageSize = iv.image.size;
CGFloat imageScale = fminf(CGRectGetWidth(iv.bounds)/imageSize.width, CGRectGetHeight(iv.bounds)/imageSize.height);
CGSize scaledImageSize = CGSizeMake(imageSize.width*imageScale, imageSize.height*imageScale);
CGRect imageFrame = CGRectMake(roundf(0.5f*(CGRectGetWidth(iv.bounds)-scaledImageSize.width)), roundf(0.5f*(CGRectGetHeight(iv.bounds)-scaledImageSize.height)), roundf(scaledImageSize.width), roundf(scaledImageSize.height));

事实上,它自iOS 4以来内置(用于UIViewContentModeScaleAspectFit ):

@import AVFoundation;

AVMakeRectWithAspectRatioInsideRect(imageView.image.size, imageView.bounds);

由于框架组件可能不是圆形数字,因此可以考虑将呼叫打包

CGRectIntegral(…)

有关更多技巧,请参阅http://nshipster.com/image-resizing/。


使用AVFoundation的这个api
#import <AVFoundation/AVFoundation.h> CGRect imageRect = AVMakeRectWithAspectRatioInsideRect(imageView.image.size, imageView.bounds);

链接地址: http://www.djcxy.com/p/81993.html

上一篇: How to get the displayed image frame from UIImageView?

下一篇: UITextView with heavy markdown support