我有一个以编程方式声明的图像视图,我也以编程方式设置它的图像。
但是,我发现自己无法将图像设置为既适合Aspect,又与图像视图的中心对齐。
换句话说,我希望图像:
我怎么才能得到那个?
只需粘贴解决方案:
就像@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; }
解决了我的问题
[your_imageview setContentMode:UIViewContentModeCenter];
更新的答案
当我最初在2014年回答这个问题时,没有要求在小图像的情况下不放大图像。(问题是2015年编辑。)如果您有这样的要求,您将确实需要将图像的大小与ImageView的大小进行比较,并在所有其他情况下使用UIViewContentModeCenter(在图像小于ImageView的情况下)或UIViewContentModeScaleAspectFit。
UIViewContentModeCenter
UIViewContentModeScaleAspectFit
原来的答案
将ImageView的ContentMode设置为UIViewContentModeScaleAspectFit对我来说就足够了。它似乎也是图像的中心。我不知道为什么其他人使用基于图像的逻辑。另请参阅此问题:IOS方面适合和中心
在Swift语言中,我们可以如下设置UIImage视图的内容模式:
let newImgThumb = UIImageView(frame: CGRect(x: 10, y: 10, width: 100, height: 100)) newImgThumb.contentMode = .scaleAspectFit
我这样解决了这个问题。
UIImageView
(CGSize imageSize = imageView.image.size)
[imageView sizeThatFits:imageSize]
我想将UIView放在UICollectionViewCell的顶部中间。 所以,我用了这个功能。
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]; }
对我有用。
您可以通过将图像视图的内容模式设置为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; }
在此编辑(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; }
let bannerImageView = UIImageView(); bannerImageView.contentMode = .ScaleAspectFit; bannerImageView.frame = CGRectMake(cftX, cftY, ViewWidth, scrollHeight);
如果图像不大于视图,则此子类使用“中心”,否则它将缩小。我发现这对于更改大小的UIImageView非常有用。
它显示的图像比大尺寸的视图小,但比小尺寸的视图大。我只想缩小规模,而不是扩大规模。
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 } } }
对于寻找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
yourImageView.contentMode = .center
您可以使用以下选项来定位图像:
scaleToFill
scaleAspectFit
redraw
center
top
bottom
left
right
topLeft
topRight
bottomLeft
bottomRight