The blue line is the imageview's bounds.
UIImageView
's contentMode
is UIViewContentModeScaleAspectFit
,
And I want keep the original picture's scale.
How can I make the picture's left edge is on the UIImageView
's left edge?
But not like UIViewContentModeTopLeft
mode. 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.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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
}
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,
};
回答2:
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
/// 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
回答3:
just like this
imageView.contentMode = UIViewContentModeCenter;
if (imageView.bounds.size.width > imageView.size.width && imageView.bounds.size.height > imageView.size.height) {
imageView.contentMode = UIViewContentModeScaleAspectFit;
}
回答4:
Swift
import UIKit
import AVFoundation
let originalsize = CGSizeMake(500, 500)
let rect = CGRectMake(0, 0, 320, 480)
var result = AVMakeRectWithAspectRatioInsideRect(originalsize, rect)
print(result)