UIImageView内容模式

jxd*_*ter 9 objective-c uiimageview ios

在此输入图像描述

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

Anb*_*hik 6

 yourimageView.contentMode = UIViewContentModeCenter;
if ( yourimageView.bounds.size.width > yourimageView.size.width && yourimageView.bounds.size.height > yourimageView.size.height) {
   yourimageView.contentMode = UIViewContentModeScaleAspectFit;
}
Run Code Online (Sandbox Code Playgroud)

Swift3

yourimageView.contentMode = .center
if yourimageView.bounds.size.width > yourimageView.size.width && yourimageView.bounds.size.height > yourimageView.size.height {
yourimageView.contentMode = .scaleAspectFit
}
Run Code Online (Sandbox Code Playgroud)

你可以改变你的模式

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,
};
Run Code Online (Sandbox Code Playgroud)