我有一个以编程方式声明的图像视图,我也是以编程方式设置其图像。
但是,我发现自己无法将图像设置为适合方面并将中心与图像视图对齐。
换句话说,我想要图像:
我如何得到它?
答案 0 :(得分:190)
刚刚粘贴解决方案:
就像@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;
}
解决了我的问题
答案 1 :(得分:67)
在swift语言中,我们可以设置UIImage视图的内容模式,如下所示:
let newImgThumb = UIImageView(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
newImgThumb.contentMode = .scaleAspectFit
答案 2 :(得分:12)
更新回答
当我在2014年最初回答这个问题时,没有要求在小图像的情况下不缩放图像。 (问题是edited in 2015。)如果您有这样的要求,您确实需要将图像的大小与imageView的大小进行比较,并使用UIViewContentModeCenter
(如果是在所有其他情况下,图像小于imageView)或UIViewContentModeScaleAspectFit
。
原始回答
将imageView的contentMode设置为UIViewContentModeScaleAspectFit
对我来说已经足够了。它似乎也是图像的中心。我不确定为什么其他人会根据图像使用逻辑。另请参阅此问题:iOS aspect fit and center
答案 3 :(得分:10)
我这样解决了这个问题。
UIImageView
(UIViewContentModeScaleAspectFit
)(CGSize imageSize = imageView.image.size)
UIImageView
调整大小。 [imageView sizeThatFits:imageSize]
我想把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];
}
它对我有用。
答案 4 :(得分:8)
您可以通过将图像视图的内容模式设置为UIViewContentModeScaleAspectFill来实现此目的。
然后使用以下方法方法获取调整大小的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;
}
在这里编辑(斯威夫特版)
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;
}
答案 5 :(得分:8)
如果图像不大于视图,则此子类使用center,否则缩小。我发现这对改变大小的 WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));
很有用。
它显示的图像小于大尺寸的视图,但大于小尺寸的视图。我希望它只能缩小,但不能提升。
UIImageView
答案 6 :(得分:7)
let bannerImageView = UIImageView();
bannerImageView.contentMode = .ScaleAspectFit;
bannerImageView.frame = CGRectMake(cftX, cftY, ViewWidth, scrollHeight);
答案 7 :(得分:5)
[your_imageview setContentMode:UIViewContentModeCenter];
答案 8 :(得分:5)
迅速:
yourImageView.contentMode = .center
您可以使用以下选项来定位图像:
scaleToFill
scaleAspectFit
//内容按比例缩放以适合固定
方面。其余部分是透明的redraw
//重绘边界更改(调用-setNeedsDisplay)center
//内容保持相同大小。调整位置。top
bottom
left
right
topLeft
topRight
bottomLeft
bottomRight
答案 9 :(得分:1)
对于寻找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