繁体   English   中英

需要有关UIImageView的contentMode和autoresizingMask的帮助

[英]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;

您将不得不使用视图的图层。

您可以在https://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/CoreAnimation_guide/Articles/Layers.html找到相应的指南。

希望这可以帮助。

谢谢,
吉姆。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM