如果UIImageView移出给定的rect,我想剪裁它。
通过设置边框,图像在屏幕上移动,但当它移出给定的固定矩形(比如(0,0650650((时,我需要对其进行剪辑。
如果您只想在UIImageView中显示图像的中心,请通过IB将内容模式设置为"Aspect Fill"。这将使其居中并裁剪掉任何多余的内容。
或在代码中
myImageView.contentMode = UIViewContentModeScaleAspectFill;
或
子类UIView,在drawRect方法中,调整rect并使用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;
您是否尝试使用UIView
的属性clipsToBounds
并将其设置为YES
,如参考中所述
Setting this value to YES causes subviews to be clipped to the bounds of the receiver
更新
看看这个SO问题是否能帮助你
正确使用UIRectClip将UIImage缩小到图标大小
myImageView.contentMode = UIViewContentModeCenter;
试试这个。