【问题标题】:Clipping UIImageView if is moved out of CGRect如果移出 CGRect 则裁剪 UIImageView
【发布时间】:2011-04-12 06:54:14
【问题描述】:

如果 UIImageView 移出给定的矩形,我想剪辑它。

图像通过设置框架在屏幕上移动,但是当它移出给定的固定矩形(例如 (0,0,650,650))时,我需要对其进行剪辑。

【问题讨论】:

  • 你在为 UIImageView 使用不同的矩形吗?尝试使用该修复矩形大小....
  • rect 大小固定.. clipsToBounds 不起作用..
  • 您要剪辑图像吗?如果它的大小大于 ImageView 的大小?。
  • 不..如果图像移出固定的 CGRect..,我需要剪辑图像..

标签: iphone uiimageview


【解决方案1】:

如果您只想在 UIImageView 中显示图像的中心,则通过 IB 将内容模式设置为“Aspect Fill”。这将使它居中并裁剪掉任何多余的东西。

或者在代码中

myImageView.contentMode = UIViewContentModeScaleAspectFill;

子类 UIView 并在 drawRect 方法中,使用CGContextDrawImage (context, rect, sourceImage.CGImage).调整矩形并绘制出裁剪图像

使用 CALayers。您可以调整 imageView 层的“contentsRect”:

CALayer* imageLayer = imageView.layer;
imageLayer.masksToBounds = YES;
CGRect rect;
if (inPortrait)
  rect = CGRectMake(0.0, 0.0, 0.5, 1.0); // half size
else 
  rect = CGRectMake(0.0, 0.0, 1.0, 1.0); // full size
imageLayer.contentsRect = rect;

【讨论】:

    【解决方案2】:

    您是否尝试过使用UIView 的属性clipsToBounds 并将其设置为YES,正如它在Reference 中所说的那样

     Setting this value to YES causes subviews to be clipped to the bounds of the receiver 
    

    更新

    看看这个 SO 问题是否对你有帮助

    Proper use of UIRectClip to scale a UIImage down to icon size

    【讨论】:

      【解决方案3】:
      myImageView.contentMode = UIViewContentModeCenter;
      

      试试这个。

      【讨论】:

        猜你喜欢
        • 2019-05-20
        • 2020-12-25
        • 2021-11-18
        • 2015-10-28
        • 2013-08-21
        • 1970-01-01
        • 1970-01-01
        • 2016-02-06
        • 2020-11-19
        相关资源
        最近更新 更多