如何按比例缩放UIImageView?

我有一个UIImageView和目标是通过给它一个高度或宽度按比例缩小它。

UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3092/2915896504_a88b69c9de.jpg"]]]; UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; //Add image view [self.view addSubview:imageView]; //set contentMode to scale aspect to fit imageView.contentMode = UIViewContentModeScaleAspectFit; //change width of frame CGRect frame = imageView.frame; frame.size.width = 100; imageView.frame = frame; 

图像确实被调整了大小,但位置不在左上angular。 缩放图像/ imageView的最佳方法是什么?如何纠正位置?

很容易修复,一旦我find了文档!

  imageView.contentMode = UIViewContentModeScaleAspectFit; 

我已经看了一些关于比例types的讨论,所以我决定把一篇关于一些最stream行的内容模式比例types的文章放在一起。

相关的图片在这里:

在这里输入图像描述

我只是试过这个,UIImage不支持_imageScaledToSize。

我最终使用一个类别向UIImage添加了一个方法 – 这是我在Apple Dev论坛上find的一个build议。

在一个项目范围内.h –

 @interface UIImage (Extras) - (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize; @end; 

执行:

 @implementation UIImage (Extras) - (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize { UIImage *sourceImage = self; UIImage *newImage = nil; CGSize imageSize = sourceImage.size; CGFloat width = imageSize.width; CGFloat height = imageSize.height; CGFloat targetWidth = targetSize.width; CGFloat targetHeight = targetSize.height; CGFloat scaleFactor = 0.0; CGFloat scaledWidth = targetWidth; CGFloat scaledHeight = targetHeight; CGPoint thumbnailPoint = CGPointMake(0.0,0.0); if (CGSizeEqualToSize(imageSize, targetSize) == NO) { CGFloat widthFactor = targetWidth / width; CGFloat heightFactor = targetHeight / height; if (widthFactor < heightFactor) scaleFactor = widthFactor; else scaleFactor = heightFactor; scaledWidth = width * scaleFactor; scaledHeight = height * scaleFactor; // center the image if (widthFactor < heightFactor) { thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; } else if (widthFactor > heightFactor) { thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; } } // this is actually the interesting part: UIGraphicsBeginImageContext(targetSize); CGRect thumbnailRect = CGRectZero; thumbnailRect.origin = thumbnailPoint; thumbnailRect.size.width = scaledWidth; thumbnailRect.size.height = scaledHeight; [sourceImage drawInRect:thumbnailRect]; newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); if(newImage == nil) NSLog(@"could not scale image"); return newImage ; } @end; 
 imageView.contentMode = UIViewContentModeScaleAspectFill; imageView.clipsToBounds = YES; 

您可以尝试使imageView大小与image匹配。 以下代码未经testing。

 CGSize kMaxImageViewSize = {.width = 100, .height = 100}; CGSize imageSize = image.size; CGFloat aspectRatio = imageSize.width / imageSize.height; CGRect frame = imageView.frame; if (kMaxImageViewSize.width / aspectRatio <= kMaxImageViewSize.height) { frame.size.width = kMaxImageViewSize.width; frame.size.height = frame.size.width / aspectRatio; } else { frame.size.height = kMaxImageViewSize.height; frame.size.width = frame.size.height * aspectRatio; } imageView.frame = frame; 
 UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3092/2915896504_a88b69c9de.jpg"]]]; UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; //set contentMode to scale aspect to fit imageView.contentMode = UIViewContentModeScaleAspectFit; //change width of frame //CGRect frame = imageView.frame; //frame.size.width = 100; //imageView.frame = frame; //original lines that deal with frame commented out, yo. imageView.frame = CGRectMake(10, 20, 60, 60); ... //Add image view [myView addSubview:imageView]; 

在iOS 4.2中,发布在顶端的原始代码非常适合我。

我发现创build一个CGRect并指定所有的顶部,左侧,宽度和高度的值是调整位置的最简单的方法,我的情况,这是在一个表格单元格内使用UIImageView。 (仍然需要添加代码来释放对象)

可以这样调整UIImage的大小

 image = [UIImage imageWithCGImage:[image CGImage] scale:2.0 orientation:UIImageOrientationUp]; 

通过select模式为Aspect Fill设置您的ImageView并检查Clip Subviews框。

在这里输入图像描述

对于Swift:

 self.imageViews.contentMode = UIViewContentMode.ScaleToFill 

这对我来说很好Swift 2.x:

 imageView.contentMode = .ScaleAspectFill imageView.clipsToBounds = true; 

的UIImageView + Scale.h:

 #import <Foundation/Foundation.h> @interface UIImageView (Scale) -(void) scaleAspectFit:(CGFloat) scaleFactor; @end 

的UIImageView + Scale.m:

 #import "UIImageView+Scale.h" @implementation UIImageView (Scale) -(void) scaleAspectFit:(CGFloat) scaleFactor{ self.contentScaleFactor = scaleFactor; self.transform = CGAffineTransformMakeScale(scaleFactor, scaleFactor); CGRect newRect = self.frame; newRect.origin.x = 0; newRect.origin.y = 0; self.frame = newRect; } @end 

设置您的UIimageview规模………

在这里输入图像描述

我用下面的code.where imageCoverView是UIView保存UIImageView

 if (image.size.height<self.imageCoverView.bounds.size.height && image.size.width<self.imageCoverView.bounds.size.width) { [self.profileImageView sizeToFit]; self.profileImageView.contentMode =UIViewContentModeCenter } else { self.profileImageView.contentMode =UIViewContentModeScaleAspectFit; } 

如果这里提出的解决scheme不适合你,而你的图像资源实际上是PDF,请注意,XCode实际上对待PDF的方式不同于图像文件。 特别是,它似乎不能扩展到正确填充PDF:它最终将平铺。 这让我疯狂,直到我发现问题是PDF格式。 转换为JPG,你应该很好去。

通常我用我的应用程序( Swift 2.x兼容)这个方法:

 // Resize UIImage func resizeImage(image:UIImage, scaleX:CGFloat,scaleY:CGFloat) ->UIImage { let size = CGSizeApplyAffineTransform(image.size, CGAffineTransformMakeScale(scaleX, scaleY)) let hasAlpha = true let scale: CGFloat = 0.0 // Automatically use scale factor of main screen UIGraphicsBeginImageContextWithOptions(size, !hasAlpha, scale) image.drawInRect(CGRect(origin: CGPointZero, size: size)) let scaledImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return scaledImage } 

我想你可以做一些像

 image.center = [[imageView window] center]; 

这里是你如何轻松地扩展它。

这与2.x模拟器和iPhone的作品。

 UIImage *thumbnail = [originalImage _imageScaledToSize:CGSizeMake(40.0, 40.0) interpolationQuality:1];