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

RAG*_*poR 7 iphone uiimageview

我有像这个图像的图片,它将出现在风景模式

替代文字

所以如果将设备旋转到Portrait,如何呈现这样的图像

替代文字

Mat*_*oal 12

使用具有以下设置的UIImageView:

imageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
                              UIViewAutoresizingFlexibleHeight);

imageView.contentMode = UIViewContentModeScaleAspectFit;
Run Code Online (Sandbox Code Playgroud)


Ram*_*min 7

看起来你要做的就是将图像剪裁在右边,同时保持顶部,左边和底部相同.

要尝试两件事:

1)子类UIView并在drawRect方法中,调整矩形并使用绘制出剪裁的图像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;
Run Code Online (Sandbox Code Playgroud)

这将图像沿宽度切成两半.masksToBounds负责剪辑子层(如果你有它们).您可以调整contentsRect单位矩形以调整您希望截止的位置.您可能还想调整imageView自己的边界以匹配大小.

这有一个额外的好处,因为调整contentsRect会自动进行动画处理,因此当你进行旋转时,宽度可以很好地进行动画制作.


Jim*_*Jim 0

您将必须使用视图的图层。

您可以在以下位置找到适当的指南:您可以在https://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/CoreAnimation_guide/Articles/Layers.html

希望这可以帮助。

谢谢,
吉姆。