14
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UIImageViewにAspectFitで画像を入れた時の実際の表示サイズを取得する方法

Posted at

縦横の比率を見て計算してもできるけどもっと簡単な方法があった。

AVFoundationのAVMakeRectメソッドを使う。

AVUtilities
public func AVMakeRect(aspectRatio: CGSize, insideRect boundingRect: CGRect) -> CGRect

使い方

// インポートする
import AVFoundation

func calculateImageSize() -> CGSize {
	let image = UIImage(named: "yourImage")!
    let imageSize = AVMakeRect(aspectRatio: image.size, insideRect: self.imageView.bounds).size
    return imageSize
}

AspectFitで画像を入れた後、UIImageViewのサイズを変えたりすれば上寄せとかできるから地味に便利。

14
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?