[英]UIImageView content mode
The blue line is the imageview's bounds. 蓝线是imageview的界限。
UIImageView
's contentMode
is UIViewContentModeScaleAspectFit
, And I want keep the original picture's scale. UIImageView
的contentMode
是UIViewContentModeScaleAspectFit
,我希望保持原始图片的比例。 How can I make the picture's left edge is on the UIImageView
's left edge? 如何让图片的左边缘位于
UIImageView
的左边缘? But not like UIViewContentModeTopLeft
mode. 但不像
UIViewContentModeTopLeft
模式。 Keep the scale and no blank in the left. 保持比例,左边没有空白。 My english is not good, hope you guys could understand what I'm saying.
我的英语不好,希望你们能理解我在说什么。 Thank you very much.
非常感谢你。
yourimageView.contentMode = UIViewContentModeCenter;
if ( yourimageView.bounds.size.width > yourimageView.size.width && yourimageView.bounds.size.height > yourimageView.size.height) {
yourimageView.contentMode = UIViewContentModeScaleAspectFit;
}
Swift3 Swift3
yourimageView.contentMode = .center
if yourimageView.bounds.size.width > yourimageView.size.width && yourimageView.bounds.size.height > yourimageView.size.height {
yourimageView.contentMode = .scaleAspectFit
}
you can change your mode as 你可以改变你的模式
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,
};
just like this 像这样
imageView.contentMode = UIViewContentModeCenter;
if (imageView.bounds.size.width > imageView.size.width && imageView.bounds.size.height > imageView.size.height) {
imageView.contentMode = UIViewContentModeScaleAspectFit;
}
the moment i saw your situation,i thought about this: 在我看到你的情况的那一刻,我想到了这个:
_imageView.contentMode = UIViewContentModeLeft | UIViewContentModeScaleAspectFit;
and i tried it, but it makes things worse 我试过了,但它让事情变得更糟
so i think you may resize your imageView`s bound to fit the size you want with this code 所以我认为您可以使用此代码调整imageView的大小,使其符合您想要的大小
/// resize image to fit in show area without changing image ratio
+ (CGSize)resizeImageViewFromImageSize:(CGSize)imageSize toFitShowSize:(CGSize)showSize {
CGFloat ratioW = imageSize.width / showSize.width;
CGFloat ratioH = imageSize.height / showSize.height;
CGFloat ratio = imageSize.width / imageSize.height;
if (ratioW > 1 && ratioH > 1) {
if (ratioW > ratioH) {
imageSize.width = showSize.width;
imageSize.height = imageSize.width / ratio;
} else {
imageSize.height = showSize.height;
imageSize.width = imageSize.height * ratio;
}
} else if (ratioW > 1) {
imageSize.width = showSize.width;
imageSize.height = imageSize.width / ratio;
} else if (ratioH > 1) {
imageSize.height = showSize.height;
imageSize.width = imageSize.height * ratio;
} else {
if (ratioW > ratioH) {
if (showSize.width / imageSize.width <= 2) {
imageSize.width = showSize.width;
imageSize.height = imageSize.width / ratio;
}
} else {
if (showSize.height / imageSize.height <= 2) {
imageSize.height = showSize.height;
imageSize.width = imageSize.height * ratio;
}
}
}
return imageSize;
}
then put your imageView align to left of the screen 然后将您的imageView对齐到屏幕左侧
Swift 迅速
import UIKit
import AVFoundation
let originalsize = CGSizeMake(500, 500)
let rect = CGRectMake(0, 0, 320, 480)
var result = AVMakeRectWithAspectRatioInsideRect(originalsize, rect)
print(result)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.