iPhone SDK - UIImageView:图像的位置

时间:2011-07-06 13:04:43

标签: iphone objective-c uiimageview

我的标准UIImageView ContentMode 设置为UIContentModeAspectRatioFit。 我试图找出如何获取图像相对于绝对坐标或UIImageView原点的位置。

这可能吗?我看过很多关于如何设置位置的帖子,但没有关于是否可以获取这个位置的信息。

非常感谢, 布雷特

4 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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/

答案 2 :(得分:3)

图像的中心等于视图的中心。您必须根据图像和视图的宽高比自行计算其他所有内容。

答案 3 :(得分:0)

你不能那样做。正如它所暗示的那样,UIImageView只显示图像。

但更一般地说,为什么要在UIImageView中添加填充?根据需要定位框架不是更简单吗?