iPhone SDK - UIImageView: 图像位置

4

我有一个标准的UIImageView,它的ContentMode设置为UIContentModeAspectRatioFit

我正在尝试找出如何获取图像相对于绝对坐标或UIImageView原点的位置。

这个可能吗?我看到了很多关于无法设置位置的帖子,但没有关于是否可以获取位置的内容。

非常感谢, Brett

4个回答

8

我知道这个问题已经有了答案,但是我编写了一个函数来计算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);
}

这是我发现的最有效的查找x和y位置的方法。太喜欢了..! - simbesi.com

4

好的,我用一些技巧解决了这个问题。

我使用了 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/


3

图像的中心点等于视图的中心点。其他所有内容都需要根据图像和视图的宽高比进行计算。


你好,能告诉我“photo”是什么吗?我的意思是这行代码中的类型:“CGSize imageInViewSize = [photo resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:imageView.size interpolationQuality:kCGInterpolationNone].size;”。由Brett回答。谢谢!@Ole Begemann - Babul
@Babul:为什么不把你的评论放在它应该在的地方(即,在Brett的答案下面)? - Ole Begemann
他现在没有看stackoverflow。他两天前看了Ole Begemann发布的stackoverflow帖子。 - Babul

0

你不能这样做。UIImageView正如其名,只显示图像,没有别的。

但更一般地说,为什么要在UIImageView内部添加填充?将框架定位到所需位置不是更简单吗?


我正在尝试向UIImageView添加一个覆盖层,但我只希望覆盖层出现在实际图像所在的位置。当contentMode设置为AspectFit时,我需要图像的实际位置。 - Brett

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接