如何在保持宽高比的同时调整UIImage的大小

我想调整UIImage大小并保持其长宽比。 我写了下面的代码,但它不能按预期工作。

 -(UIImage * ) scaleImage: (UIImage * ) image toSize: (CGSize) targetSize { CGFloat scaleFactor = 1.0; if (image.size.width > targetSize.width || image.size.height > targetSize.height) if (!((scaleFactor = (targetSize.width / image.size.width)) > (targetSize.height / image.size.height))) //scale to fit width, or scaleFactor = targetSize.height / image.size.height; // scale to fit heigth. UIGraphicsBeginImageContext(targetSize); CGRect rect = CGRectMake((targetSize.width - image.size.width * scaleFactor) / 2, (targetSize.height - image.size.height * scaleFactor) / 2, image.size.width * scaleFactor, image.size.height * scaleFactor); [image drawInRect: rect]; UIImage * scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return scaledImage; } 

这到底是什么错误?

我在几个项目中使用类似的东西:

 - (UIImage*) scaleImage:(UIImage*)image toSize:(CGSize)newSize { CGSize scaledSize = newSize; float scaleFactor = 1.0; if( image.size.width > image.size.height ) { scaleFactor = image.size.width / image.size.height; scaledSize.width = newSize.width; scaledSize.height = newSize.height / scaleFactor; } else { scaleFactor = image.size.height / image.size.width; scaledSize.height = newSize.height; scaledSize.width = newSize.width / scaleFactor; } UIGraphicsBeginImageContextWithOptions( scaledSize, NO, 0.0 ); CGRect scaledImageRect = CGRectMake( 0.0, 0.0, scaledSize.width, scaledSize.height ); [image drawInRect:scaledImageRect]; UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return scaledImage; } 

如果要在UI中显示,则可以使用Interface Builder并指定“Aspect Fit”属性。

您也可以通过将内容模式设置为UIViewContentModeScaleAspectFit来在代码中执行此操作:

 imageView.contentMode = UIViewContentModeScaleAspectFit; 

这种方法需要一个图像和一个最大维度。 如果原始图像的最大尺寸小于指定的最大尺寸,则会得到原始图像,因此不会被炸毁。 否则,您会得到一个新的图像,其最大尺寸为指定的一个,另一个由原始的宽高比决定。 举个例子,如果你给它一个1024×768的图片,640的最大尺寸,你会得到一个640×480的版本,如果你给它一个768×1024的图片,640的最大尺寸,你会得到一个480×640的版本。

 - (UIImage *)resizeImage:(UIImage *)image withMaxDimension:(CGFloat)maxDimension { if (fmax(image.size.width, image.size.height) <= maxDimension) { return image; } CGFloat aspect = image.size.width / image.size.height; CGSize newSize; if (image.size.width > image.size.height) { newSize = CGSizeMake(maxDimension, maxDimension / aspect); } else { newSize = CGSizeMake(maxDimension * aspect, maxDimension); } UIGraphicsBeginImageContextWithOptions(newSize, NO, 1.0); CGRect newImageRect = CGRectMake(0.0, 0.0, newSize.width, newSize.height); [image drawInRect:newImageRect]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } 

在我的情况(我想在UIView中有一个图像),并保持与自动布局的比例在这里描述的方式是伟大的。 这篇文章对我来说最重要的部分是(几乎引用):

 constraint = [NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.imageView attribute:NSLayoutAttributeWidth multiplier:ration constant:0.0f]; [self.imageView addConstraint:constraint]; 

唯一要根据图像的实际宽度和高度来计算比例。 我发现这种方式容易和优雅。

 - (UIImage *)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize { UIGraphicsBeginImageContext(newSize); float ratio = newSize.width/image.size.width; [image drawInRect:CGRectMake(0, 0, newSize.width, ratio * image.size.height)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); NSLog(@"New Image Size : (%f, %f)", newImage.size.width, newImage.size.height); UIGraphicsEndImageContext(); return newImage; }