search

Home  >  Q&A  >  body text

ios - UIImageView如何让图片自适应大小而且居右显示呢?

如果我选择了UIViewContentModeScaleAspectFit这个自适应模式。大图图片正好自适应,但是居中显示了。

如果我选择UIViewContentModeRight居右模式。图片居右了。但是如果是大图。会超出容器,因为他没有自适应效果。

这个问题如何解决呢?

PHPzPHPz2773 days ago886

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-04-17 15:29:06

    1. I don’t know if ContentMode supports OR operation, that is (you need to test this method),
      view.contentMode = UIViewContentModeScaleAspectFit|UIViewContentModeRight;
    2. You can extend the UIImage method and crop the UIImage content starting from the right side,

      + (UIImage *)cropFromRight:(CGSize)size
      {
          // size是 UIImageView的大小
          // 根据宽高比,换算出裁剪区域大小,并执行裁剪然后返回 UIImage
      
      }
      

      Similarly, this method can be extended to center cropping, cropping from top, cropping from bottom, etc.

    3. Settings:
      view.layer.masksToBounds = YES;//内容超出控件将不进行显示

    reply
    0
  • Cancelreply