UIImageView内容模式

时间:2014-10-16 09:06:25

标签: ios objective-c uiimageview

enter image description here

蓝线是imageview的界限。 UIImageView contentModeUIViewContentModeScaleAspectFit, 我希望保持原始图片的规模。 如何让图片的左边缘位于UIImageView的左边缘? 但不像UIViewContentModeTopLeft模式。保持比例,左边没有空白。 我的英语不好,希望你们能理解我所说的话。 非常感谢你。

4 个答案:

答案 0 :(得分:6)

 yourimageView.contentMode = UIViewContentModeCenter;
if ( yourimageView.bounds.size.width > yourimageView.size.width && yourimageView.bounds.size.height > yourimageView.size.height) {
   yourimageView.contentMode = UIViewContentModeScaleAspectFit;
}

<强> Swift3

yourimageView.contentMode = .center
if yourimageView.bounds.size.width > yourimageView.size.width && yourimageView.bounds.size.height > yourimageView.size.height {
yourimageView.contentMode = .scaleAspectFit
}

您可以将模式更改为

typedef NS_ENUM(NSInteger, UIViewContentMode) {
UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit,      // contents scaled to fit with fixed aspect. remainder is transparent
UIViewContentModeScaleAspectFill,     // contents scaled to fill with fixed aspect. some portion of content may be clipped.
UIViewContentModeRedraw,              // redraw on bounds change (calls -setNeedsDisplay)
UIViewContentModeCenter,              // contents remain same size. positioned adjusted.
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
};

答案 1 :(得分:0)

就像这样

imageView.contentMode = UIViewContentModeCenter;
if (imageView.bounds.size.width > imageView.size.width && imageView.bounds.size.height > imageView.size.height) {
   imageView.contentMode = UIViewContentModeScaleAspectFit;
}

答案 2 :(得分:0)

在我看到你的情况的那一刻,我想到了这一点:

r

我试过了,但它让事情变得更糟

所以我认为您可以使用此代码调整imageView的大小,使其符合您想要的大小

_imageView.contentMode = UIViewContentModeLeft | UIViewContentModeScaleAspectFit;

然后将imageView对齐到屏幕左侧

答案 3 :(得分:0)

夫特

import UIKit
import AVFoundation



let originalsize = CGSizeMake(500, 500)
let rect = CGRectMake(0, 0, 320, 480)


var result = AVMakeRectWithAspectRatioInsideRect(originalsize, rect)

print(result)