3

私はいくつか持っておりUIImage、それを に表示しUIImageViewます。

UIViewContentModeScaleAspectFitコンテンツ モードを使用する必要があります。

簡単なコードは次のとおりです。

UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
imageView.image = self.image;
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
[imageView release];

画面に表示されている画像のサイズを知る方法は? 解決策、プロパティはありますか、それとも手動で計算しますか?

編集:

私はその物件について知っていますimage.size

self.image.size->>> 元の画像サイズ

imageView.image.size->>> imageView のサイズですが、表示された画像ではありません。

imageView's size表示されているサイズは、商品によって異なりますのでお尋ねしますcontentmode

4

4 に答える 4

5

画像ビューのセットにUIImageView基づいて、表示された画像の境界を内省するために使用できるカテゴリを次に示します。UIViewContentMode

@implementation UIImageView (JRAdditions)

- (CGRect)displayedImageBounds {
    UIImage *image = [self image];
    if(self.contentMode != UIViewContentModeScaleAspectFit || !image)
        return CGRectInfinite;

    CGFloat boundsWidth  = [self bounds].size.width,
            boundsHeight = [self bounds].size.height;

    CGSize  imageSize  = [image size];
    CGFloat imageRatio = imageSize.width / imageSize.height;
    CGFloat viewRatio  = boundsWidth / boundsHeight;

    if(imageRatio < viewRatio) {
        CGFloat scale = boundsHeight / imageSize.height;
        CGFloat width = scale * imageSize.width;
        CGFloat topLeftX = (boundsWidth - width) * 0.5;
        return CGRectMake(topLeftX, 0, width, boundsHeight);
    }

    CGFloat scale = boundsWidth / imageSize.width;
    CGFloat height = scale * imageSize.height;
    CGFloat topLeftY = (boundsHeight - height) * 0.5;

    return CGRectMake(0, topLeftY, boundsWidth, height);
}

@end
于 2013-03-16T10:03:29.827 に答える
4

素早く、@ jacob-relkin と同じ

extension UIImageView {

    func displayedImageBounds() -> CGRect {

        let boundsWidth = bounds.size.width
        let boundsHeight = bounds.size.height
        let imageSize = image!.size
        let imageRatio = imageSize.width / imageSize.height
        let viewRatio = boundsWidth / boundsHeight
        if ( viewRatio > imageRatio ) {
            let scale = boundsHeight / imageSize.height
            let width = scale * imageSize.width
            let topLeftX = (boundsWidth - width) * 0.5
            return CGRectMake(topLeftX, 0, width, boundsHeight)
        }
        let scale = boundsWidth / imageSize.width
        let height = scale * imageSize.height
        let topLeftY = (boundsHeight - height) * 0.5
        return CGRectMake(0,topLeftY, boundsWidth,height)
    }
}
于 2015-04-05T15:36:40.870 に答える
0

以下の方法を使用して取得することもできます。

-(CGSize)imageSizeAfterAspectFit:(UIImageView*)imgview{


float newwidth;
float newheight;

UIImage *image=imgview.image;

if (image.size.height>=image.size.width){
    newheight=imgview.frame.size.height;
    newwidth=(image.size.width/image.size.height)*newheight;

    if(newwidth>imgview.frame.size.width){
        float diff=imgview.frame.size.width-newwidth;
        newheight=newheight+diff/newheight*newheight;
        newwidth=imgview.frame.size.width;
    }

}
else{
    newwidth=imgview.frame.size.width;
    newheight=(image.size.height/image.size.width)*newwidth;

    if(newheight>imgview.frame.size.height){
        float diff=imgview.frame.size.height-newheight;
        newwidth=newwidth+diff/newwidth*newwidth;
        newheight=imgview.frame.size.height;
    }
}

NSLog(@"image after aspect fit: width=%f height=%f",newwidth,newheight);

return CGSizeMake(newwidth, newheight);

}
于 2013-03-16T10:28:11.503 に答える
0

不可能です。次の方法で元の画像サイズを取得できます

UIImage *image = imageView.image;
CGSize size = image.size;

サイズ

向きを考慮した画像の寸法。(読み取り専用)

@property(nonatomic, readonly) CGSize size
于 2013-03-16T09:52:55.863 に答える