ios UIImageView 方面适合和居中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15499376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIImageView aspect fit and center
提问by Ravi Vooda
I have an image view, declared programmatically, and I am setting its image, also programmatically.
我有一个以编程方式声明的图像视图,并且我正在以编程方式设置它的图像。
However, I find myself unable to set the image to both fit the aspect and align centre to the image view.
但是,我发现自己无法将图像设置为既适合外观又使中心与图像视图对齐。
In other words, I want the image to:
换句话说,我希望图像:
- Scale down to fit the aspect, if the image is large.
- Centre but not scale up, if the image is small.
- 如果图像很大,请缩小以适应外观。
- 如果图像很小,则居中但不放大。
How do I get that?
我怎么得到它?
回答by Ravi Vooda
Just pasting the solution:
只需粘贴解决方案:
Just like @manohar said
就像@manohar 说的
imageView.contentMode = UIViewContentModeCenter;
if (imageView.bounds.size.width > ((UIImage*)imagesArray[i]).size.width && imageView.bounds.size.height > ((UIImage*)imagesArray[i]).size.height) {
imageView.contentMode = UIViewContentModeScaleAspectFit;
}
solved my problem
解决了我的问题
回答by Ali Raza
In swift language we can set content mode of UIImage view like following as:
在 swift 语言中,我们可以设置 UIImage 视图的内容模式,如下所示:
let newImgThumb = UIImageView(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
newImgThumb.contentMode = .scaleAspectFit
回答by Jonas Deichelmann
Swift
迅速
yourImageView.contentMode = .center
You can use the following options to position your image:
您可以使用以下选项来定位您的图像:
scaleToFill
scaleAspectFit
// contents scaled to fit with fixed aspect. remainder is transparentredraw
// redraw on bounds change (calls -setNeedsDisplay)center
// contents remain same size. positioned adjusted.top
bottom
left
right
topLeft
topRight
bottomLeft
bottomRight
scaleToFill
scaleAspectFit
// 缩放内容以适应固定方面。剩余部分是透明的redraw
// 在边界改变时重绘(调用 -setNeedsDisplay)center
// 内容保持相同的大小。位置调整。top
bottom
left
right
topLeft
topRight
bottomLeft
bottomRight
回答by Nate Cook
Updated answer
更新答案
When I originally answered this question in 2014, there was no requirement to not scale the image up in the case of a small image. (The question was edited in 2015.) If you have such a requirement, you will indeed need to compare the image's size to that of the imageView and use either UIViewContentModeCenter
(in the case of an image smaller than the imageView) or UIViewContentModeScaleAspectFit
in all other cases.
我在 2014 年最初回答这个问题时,没有要求在小图像的情况下不放大图像。(该问题已在 2015年编辑。)如果您有这样的要求,您确实需要将图像的大小与 imageView 的大小进行比较,并使用UIViewContentModeCenter
(在图像小于 imageView 的UIViewContentModeScaleAspectFit
情况下)或在所有其他情况下.
Original answer
原答案
Setting the imageView's contentMode to UIViewContentModeScaleAspectFit
was enough for me. It seems to center the images as well. I'm not sure why others are using logic based on the image. See also this question: iOS aspect fit and center
将 imageView 的 contentMode 设置UIViewContentModeScaleAspectFit
为对我来说就足够了。它似乎也将图像居中。我不确定为什么其他人使用基于图像的逻辑。另见这个问题:iOS aspect fit and center
回答by Nicejinux
I solved this problem like this.
我是这样解决这个问题的。
- setImage to
UIImageView
(withUIViewContentModeScaleAspectFit
) - get imageSize
(CGSize imageSize = imageView.image.size)
UIImageView
resize.[imageView sizeThatFits:imageSize]
- move position where you want.
- 将图像设置为
UIImageView
(与UIViewContentModeScaleAspectFit
) - 获取图像大小
(CGSize imageSize = imageView.image.size)
UIImageView
调整大小。[imageView sizeThatFits:imageSize]
- 移动你想要的位置。
I wanted to put UIView on the top center of UICollectionViewCell
.
so, I used this function.
我想把 UIView 放在UICollectionViewCell
. 所以,我使用了这个功能。
- (void)setImageToCenter:(UIImageView *)imageView
{
CGSize imageSize = imageView.image.size;
[imageView sizeThatFits:imageSize];
CGPoint imageViewCenter = imageView.center;
imageViewCenter.x = CGRectGetMidX(self.contentView.frame);
[imageView setCenter:imageViewCenter];
}
It works for me.
这个对我有用。
回答by Manish
You can achieve this by setting content mode of image view to UIViewContentModeScaleAspectFill.
您可以通过将图像视图的内容模式设置为 UIViewContentModeScaleAspectFill 来实现这一点。
Then use following method method to get the resized uiimage object.
然后使用以下方法获取调整大小的 uiimage 对象。
- (UIImage*)setProfileImage:(UIImage *)imageToResize onImageView:(UIImageView *)imageView
{
CGFloat width = imageToResize.size.width;
CGFloat height = imageToResize.size.height;
float scaleFactor;
if(width > height)
{
scaleFactor = imageView.frame.size.height / height;
}
else
{
scaleFactor = imageView.frame.size.width / width;
}
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width * scaleFactor, height * scaleFactor), NO, 0.0);
[imageToResize drawInRect:CGRectMake(0, 0, width * scaleFactor, height * scaleFactor)];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resizedImage;
}
Edited Here (Swift Version)
在这里编辑(Swift 版)
func setProfileImage(imageToResize: UIImage, onImageView: UIImageView) -> UIImage
{
let width = imageToResize.size.width
let height = imageToResize.size.height
var scaleFactor: CGFloat
if(width > height)
{
scaleFactor = onImageView.frame.size.height / height;
}
else
{
scaleFactor = onImageView.frame.size.width / width;
}
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width * scaleFactor, height * scaleFactor), false, 0.0)
imageToResize.drawInRect(CGRectMake(0, 0, width * scaleFactor, height * scaleFactor))
let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return resizedImage;
}
回答by orkoden
This subclass uses center if the image is not larger than the view, otherwise it scales down. I found this useful for a UIImageView
that changes the size.
如果图像不大于视图,则此子类使用 center ,否则按比例缩小。我发现这对于UIImageView
改变大小的一个很有用。
The image it displays is smaller than the view for large sizes, but larger than the view for small sizes. I want it only to scale down, but not up.
它显示的图像比大尺寸的视图小,但比小尺寸的视图大。我只希望它缩小,而不是放大。
class CenterScaleToFitImageView: UIImageView {
override var bounds: CGRect {
didSet {
adjustContentMode()
}
}
override var image: UIImage? {
didSet {
adjustContentMode()
}
}
func adjustContentMode() {
guard let image = image else {
return
}
if image.size.width > bounds.size.width ||
image.size.height > bounds.size.height {
contentMode = .ScaleAspectFit
} else {
contentMode = .Center
}
}
}
回答by Antony Ouseph
let bannerImageView = UIImageView();
bannerImageView.contentMode = .ScaleAspectFit;
bannerImageView.frame = CGRectMake(cftX, cftY, ViewWidth, scrollHeight);
回答by souvickcse
[your_imageview setContentMode:UIViewContentModeCenter];
回答by karim
For people looking for UIImage resizing,
对于正在寻找调整 UIImage 大小的人,
@implementation UIImage (Resize)
- (UIImage *)scaledToSize:(CGSize)size
{
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
[self drawInRect:CGRectMake(0.0f, 0.0f, size.width, size.height)];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (UIImage *)aspectFitToSize:(CGSize)size
{
CGFloat aspect = self.size.width / self.size.height;
if (size.width / aspect <= size.height)
{
return [self scaledToSize:CGSizeMake(size.width, size.width / aspect)];
} else {
return [self scaledToSize:CGSizeMake(size.height * aspect, size.height)];
}
}
- (UIImage *)aspectFillToSize:(CGSize)size
{
CGFloat imgAspect = self.size.width / self.size.height;
CGFloat sizeAspect = size.width/size.height;
CGSize scaledSize;
if (sizeAspect > imgAspect) { // increase width, crop height
scaledSize = CGSizeMake(size.width, size.width / imgAspect);
} else { // increase height, crop width
scaledSize = CGSizeMake(size.height * imgAspect, size.height);
}
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClipToRect(context, CGRectMake(0, 0, size.width, size.height));
[self drawInRect:CGRectMake(0.0f, 0.0f, scaledSize.width, scaledSize.height)];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end