簡體   English   中英

iPhone SDK - UIImageView:圖像的 position

[英]iPhone SDK - UIImageView: position of image

我有一個標准的UIImageView ,其ContentMode設置為UIContentModeAspectRatioFit 我試圖找出如何獲得相對於絕對坐標或UIImageView原點的圖像的 position。

這可能嗎? 我看過很多關於如何設置 position 的帖子,但沒有關於是否可以獲取position 的帖子。

非常感謝,布雷特

我知道這個問題已經得到解答,但我寫了一個 function 來計算 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 function 將圖像大小調整為 UIImageView 的大小。 然后我保留了圖像大小並取消了圖像(我只需要大小,而不需要圖像本身)。 接下來是一些簡單的數學運算,我們得到了框架中圖像的 position!

    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 中添加填充? 根據需要對框架進行 position 不是更簡單嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM