剪切UIImageView if是否移出CGRect

时间:2011-04-12 06:54:14

标签: iphone uiimageview

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

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

3 个答案:

答案 0 :(得分:0)

如果您只想显示图像的中心,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;

答案 1 :(得分:0)

您是否尝试使用clipsToBounds的{​​{1}}属性并将其设置为UIView,如Reference中所述那样

YES

<强>更新

看看这个SO问题是否有助于你

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

答案 2 :(得分:0)

myImageView.contentMode = UIViewContentModeCenter;

试试这个。