xcode IOS:调整 UIImage 大小的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7417257/
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
IOS: problem with resize a UIImage
提问by cyclingIsBetter
I wanto to resize an UIImage and I try this code:
我想调整 UIImage 的大小,然后尝试以下代码:
UIImage *newImage = [image1toResize _imageScaledToSize:CGSizeMake(290, 390) interpolationQuality:1];
but I have this warning: warning: 'UIImage' may not respond to '-_imageScaledToSize:'
但我有这个警告:警告:'UIImage' 可能不会响应'-_imageScaledToSize:'
why?
为什么?
回答by user523234
You did not ask for it but just in case you are interested in how to... Here is a very simple code to do it:
您没有要求它,但以防万一您对如何...感兴趣,这是一个非常简单的代码:
CGSize newSize = CGSizeMake(1024, 768); //whaterver size
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
回答by ThePunisher
You can Resize by doiing:
您可以通过执行以下操作来调整大小:
+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
//UIGraphicsBeginImageContext(newSize);
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
回答by Dancreek
_imageScaledToSize is not a UIImage function. If this function is part of a 3rd Party library you are using then you need to make sure that you #import the right headers in your file.
_imageScaledToSize 不是 UIImage 函数。如果此函数是您正在使用的第 3 方库的一部分,那么您需要确保在文件中#import 正确的标题。
回答by jamapag
回答by Javier Soto
You can use these categories on UIImage
to manipulate UIImage
objects (to resize them or crop them, for instance):
您可以使用这些类别UIImage
来操作UIImage
对象(例如调整它们的大小或裁剪它们):
http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/
http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/
回答by codeburn
Best way to scale images without losing the aspect ratio (i.e. without stretching the imgage) is to use this method:
在不丢失纵横比(即不拉伸图像)的情况下缩放图像的最佳方法是使用以下方法:
//to scale images without changing aspect ratio
+ (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)newSize {
+ (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)newSize {
float width = newSize.width;
float height = newSize.height;
UIGraphicsBeginImageContext(newSize);
CGRect rect = CGRectMake(0, 0, width, height);
float widthRatio = image.size.width / width;
float heightRatio = image.size.height / height;
float divisor = widthRatio > heightRatio ? widthRatio : heightRatio;
width = image.size.width / divisor;
height = image.size.height / divisor;
rect.size.width = width;
rect.size.height = height;
//indent in case of width or height difference
float offset = (width - height) / 2;
if (offset > 0) {
rect.origin.y = offset;
}
else {
rect.origin.x = -offset;
}
[image drawInRect: rect];
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return smallImage;
}
}
Add this method to your Utility class so you can use it throughout your project, and access it like so:
将此方法添加到您的 Utility 类,以便您可以在整个项目中使用它,并像这样访问它:
xyzImageView.image = [Utility scaleImage:yourUIImage toSize:xyzImageView.frame.size];
This method takes care of scaling while maintaining aspect ratio. It also adds indents to the image in case the scaled down image has more width than height (or vice versa).
此方法在保持纵横比的同时负责缩放。它还为图像添加缩进,以防缩小图像的宽度大于高度(反之亦然)。