LoginSignup
7
7

More than 5 years have passed since last update.

UIImageViewでUIViewContentModeScaleAspectFillを設定した時に画像が領域外にはみ出てしまう問題

Last updated at Posted at 2014-07-25

これたぶん常識で今まで知らなかった自分がおかしいのかもなのですが、

プログラム上でUIImageViewを生成した時に、画像をなるべく大きく表示するためにcontentMode = UIViewContentModeScaleAspectFillにするとUIImageViewの領域外に画像がはみ出てしまうことがあります。

20130828214159.png
ざっくり図にするとこんな感じ。

長方形の画像を正方形で表示させたい時とかその場合には都度UIImageを正方形にトリミングしてUIImageViewに突っ込むみたいなダルイこ
とをしていたのですが、UIImageViewにありました、綺麗に表示させる方法が・・・。

UIImageView *imageView = [UIImageView alloc] init];
imageView.frame = CGRectMake(0,0,100,100);
imageView.contentMode = UIViewContentModeScaleAspectFill;
[imageView setClipsToBounds:YES];
[self.view addSubView:imageView];

という感じでsetClipsToBoundsをYESにすれば勝手にやってくれるようです。
なんでこれデフォルトでYESになっていないのか・・・。

ちなみに大量に行うとメモリ的にあまりよろしくないらしいので、可能であればきっちり切り取った元画像を用意しておいた方が良いようです。

参考リンク : ios - UIViewContentModeScaleAspectFill not clipping - Stack Overflow

7
7
2

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
7
7