28

私は自分の UIImageView を持っていて、次のようにサイズを変更する画像を入れました:

UIImageView *attachmentImageNew = [[UIImageView alloc] initWithFrame:CGRectMake(5.5, 6.5, 245, 134)];
attachmentImageNew.image = actualImage;
attachmentImageNew.backgroundColor = [UIColor redColor];
attachmentImageNew.contentMode = UIViewContentModeScaleAspectFit;

これを行うことで、サイズ変更された画像の幅を取得しようとしましたUIImageView:

NSLog(@"Size of pic is %f", attachmentImageNew.image.size.width);

しかし、実際には元の画像の幅が返されます。画面に表示される画像のフレームを取得するにはどうすればよいですか?

編集:これが私のUIImageView外見です。赤い部分がその部分ですbackgroundColor

ここに画像の説明を入力

4

6 に答える 6

47

もっと明確な解決策があるかどうかはわかりませんが、これは機能します:

float widthRatio = imageView.bounds.size.width / imageView.image.size.width;
float heightRatio = imageView.bounds.size.height / imageView.image.size.height;
float scale = MIN(widthRatio, heightRatio);
float imageWidth = scale * imageView.image.size.width;
float imageHeight = scale * imageView.image.size.height;
于 2013-02-01T15:31:31.360 に答える
1

Swift での解決策:

let currentHeight = imageView.bounds.size.height
let currentWidth = imageView.bounds.size.width
let newWidth = UIScreen.mainScreen().bounds.width
let newHeight = (newWidth * currentHeight) / currentWidth

println(newHeight)
于 2015-08-01T09:23:49.997 に答える
0

元の画像の参照があるため、常に元の画像と同じ寸法になります。

新しい画像のサイズを取得するには、アスペクト比を確認する必要があります。プレビューを使用して、さまざまなサイズのさまざまな画像を使用して、アスペクト比に応じて画像のサイズを変更する必要がある式を導き出しました。

于 2013-02-01T14:05:45.620 に答える
0

UIViewContentModeScaleAspectFitに関する Apple のドキュメントによると

アスペクト比を維持することにより、コンテンツ(あなたの場合はactualImage)をビュー(あなたの場合はattachmentImageNew)のサイズに合わせてスケーリングします。

つまり、(スケーリング後の) 画像サイズは と同じである必要がありますUIImageView

アップデート :

使用したくない場合UIViewContentModeScaleAspectFit、およびサイズを修正できる場合は、scaledImage以下のコードを使用して newSize を修正するために画像を拡大縮小し、幅をコードに使用できます。

CGSize newSize = CGSizeMake(100.0,50.0);
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
于 2013-02-01T14:15:42.663 に答える
0

Swift 2の場合、このコード スニペットを使用して、aspectFitted画像を画面の下部に揃えることができます(この質問に対する以前の回答からサンプリング -> 皆さんに感謝します)

let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = CGFloat(screenSize.width)
let screenHeight = CGFloat(screenSize.height)

imageView.frame = CGRectMake(0, screenHeight - (imageView.image?.size.height)! , screenWidth, (imageView.image?.size.height)!)

let newSize:CGSize = getScaledSizeOfImage(imageView.image!, toSize: self.view.frame.size)

imageView.frame = CGRectMake(0, screenHeight - (newSize.height) , screenWidth, newSize.height)

func getScaledSizeOfImage(image: UIImage, toSize: CGSize) -> CGSize       
{
    let widthRatio = toSize.width/image.size.width
    let heightRatio = toSize.height/image.size.height
    let scale = min(widthRatio, heightRatio)
    let imageWidth = scale*image.size.width
    let imageHeight = scale*image.size.height
    return CGSizeMake(imageWidth, imageHeight)
}
于 2015-11-04T10:23:10.933 に答える
0

あなたの場合:

float yourScaledImageWitdh =  attachmentImageNew.frame.size.height *  attachmentImageNew.image.size.width / attachmentImageNew.image.size.height
NSLog(@"width of resized pic is %f", yourScaledImageWitdh);

ただし、前に、元の画像の比率とimageViewの比率を確認する必要があります。元の画像の比率がimageViewフレームの比率よりも広い場合ではなく、赤い領域が水平に追加された場合に備えて、私のコード行は適切です

于 2013-02-01T14:41:51.087 に答える