我有一个标准的UIImageView
,它的ContentMode设置为UIContentModeAspectRatioFit
。
我正在尝试找出如何获取图像相对于绝对坐标或UIImageView
原点的位置。
这个可能吗?我看到了很多关于无法设置位置的帖子,但没有关于是否可以获取位置的内容。
非常感谢, Brett
我有一个标准的UIImageView
,它的ContentMode设置为UIContentModeAspectRatioFit
。
我正在尝试找出如何获取图像相对于绝对坐标或UIImageView
原点的位置。
这个可能吗?我看到了很多关于无法设置位置的帖子,但没有关于是否可以获取位置的内容。
非常感谢, Brett
我知道这个问题已经有了答案,但是我编写了一个函数来计算ImageView中图像相对于ImageView的框架。
编辑:忘记了最重要的一些。
+ (CGRect) imagePositionInImageView:(UIImageView*)imageView
{
float x = 0.0f;
float y = 0.0f;
float w = 0.0f;
float h = 0.0f;
CGFloat ratio = 0.0f;
CGFloat horizontalRatio = imageView.frame.size.width / imageView.image.size.width;
CGFloat verticalRatio = imageView.frame.size.height / imageView.image.size.height;
switch (imageView.contentMode) {
case UIViewContentModeScaleToFill:
w = imageView.frame.size.width;
h = imageView.frame.size.height;
break;
case UIViewContentModeScaleAspectFit:
// contents scaled to fit with fixed aspect. remainder is transparent
ratio = MIN(horizontalRatio, verticalRatio);
w = imageView.image.size.width*ratio;
h = imageView.image.size.height*ratio;
x = (horizontalRatio == ratio ? 0 : ((imageView.frame.size.width - w)/2));
y = (verticalRatio == ratio ? 0 : ((imageView.frame.size.height - h)/2));
break;
case UIViewContentModeScaleAspectFill:
// contents scaled to fill with fixed aspect. some portion of content may be clipped.
ratio = MAX(horizontalRatio, verticalRatio);
w = imageView.image.size.width*ratio;
h = imageView.image.size.height*ratio;
x = (horizontalRatio == ratio ? 0 : ((imageView.frame.size.width - w)/2));
y = (verticalRatio == ratio ? 0 : ((imageView.frame.size.height - h)/2));
break;
case UIViewContentModeCenter:
// contents remain same size. positioned adjusted.
w = imageView.image.size.width;
h = imageView.image.size.height;
x = (imageView.frame.size.width - w)/2;
y = (imageView.frame.size.height - h)/2;
break;
case UIViewContentModeTop:
w = imageView.image.size.width;
h = imageView.image.size.height;
x = (imageView.frame.size.width - w)/2;
break;
case UIViewContentModeBottom:
w = imageView.image.size.width;
h = imageView.image.size.height;
y = (imageView.frame.size.height - h);
x = (imageView.frame.size.width - w)/2;
break;
case UIViewContentModeLeft:
w = imageView.image.size.width;
h = imageView.image.size.height;
y = (imageView.frame.size.height - h)/2;
break;
case UIViewContentModeRight:
w = imageView.image.size.width;
h = imageView.image.size.height;
y = (imageView.frame.size.height - h)/2;
x = (imageView.frame.size.width - w);
break;
case UIViewContentModeTopLeft:
w = imageView.image.size.width;
h = imageView.image.size.height;
break;
case UIViewContentModeTopRight:
w = imageView.image.size.width;
h = imageView.image.size.height;
x = (imageView.frame.size.width - w);
break;
case UIViewContentModeBottomLeft:
w = imageView.image.size.width;
h = imageView.image.size.height;
y = (imageView.frame.size.height - h);
break;
case UIViewContentModeBottomRight:
w = imageView.image.size.width;
h = imageView.image.size.height;
y = (imageView.frame.size.height - h);
x = (imageView.frame.size.width - w);
default:
break;
}
return CGRectMake(x, y, w, h);
}
好的,我用一些技巧解决了这个问题。
我使用了 Trevor Harmon 的图像 resizedImageWithContentMode
函数将图像调整为 UIImageView 的大小。然后我保留了图像大小并舍弃了图像本身(我只需要大小,而不是图像本身)。接着进行了一些简单的数学计算,我们就得到了图像在框架中的位置!
CGSize imageInViewSize = [photo resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:imageView.size interpolationQuality:kCGInterpolationNone].size;
CGRect overlayRect = CGRectMake((imageView.frame.size.width - imageInViewSize.width) / 2,
(imageView.frame.size.height - imageInViewSize.height) / 2,
imageInViewSize.width,
imageInViewSize.height);
NSLog(@"Frame of Image inside UIImageView: Left:%f Top:%f Width:%f Height:%f \n", overlayRect.origin.x, overlayRect.origin.y, overlayRect.size.width, overlayRect.size.height);
提示:Trevor Harmon的代码在此处:http://vocaro.com/trevor/blog/
图像的中心点等于视图的中心点。其他所有内容都需要根据图像和视图的宽高比进行计算。
你不能这样做。UIImageView正如其名,只显示图像,没有别的。
但更一般地说,为什么要在UIImageView内部添加填充?将框架定位到所需位置不是更简单吗?