![](/img/trans.png)
[英]UIImageView autoresizingmask, UIImage stretchableImageWithLeftCapWidth
[英]Need help about contentMode and autoresizingMask of UIImageView
我有像这个图像的图片,它将出现在风景模式
所以如果将设备旋转到Portrait,如何呈现这样的图像
使用具有以下设置的UIImageView:
imageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight);
imageView.contentMode = UIViewContentModeScaleAspectFit;
看起来你要做的就是将图像剪裁在右边,同时让顶部,左边和底部保持一致。
要尝试两件事:
1)子类UIView并在drawRect
方法中,调整rect并使用CGContextDrawImage (context, rect, sourceImage.CGImage)
绘制剪切的图像。
2)使用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;
这将图像沿宽度切成两半。 masksToBounds
负责剪切子图层(如果你有它们)。 您可以调整contentsRect
单位矩形以调整您希望截止的位置。 您可能还想调整imageView自己的边界以匹配大小。
这有一个额外的好处,因为调整contentsRect会自动进行动画处理,因此当你进行旋转时,宽度可以很好地进行动画制作。
// Set the layer contents
view.layer.contents = [image CGImage];
// Stretch so that contents overflows the edges and keeps aspect ratio
view.layer.contentsGravity = kCAGravityResizeAspectFill;
// Clip off the overflow
view.layer.masksToBounds = YES;
// Set the frame
CGRect frame;
frame.origin = CGPointZero;
frame.size = view.superview.bounds.size;
view.frame = frame;
// Make sure it resizes when the superview does
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
您将不得不使用视图的图层。
希望这可以帮助。
谢谢,
吉姆。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.