ios 调整图像大小以适合 UIImageView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12552785/
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
Resizing image to fit UIImageView
提问by cristi71000
I am very new to objective c and I'm just getting my bearings. I want to do something really simple but it proves to be quite a challenge:
我对目标 c 很陌生,我只是在了解自己的方向。我想做一些非常简单的事情,但事实证明这是一个很大的挑战:
I am trying to display an image into an UIImageView
. The image I'm showing is large and I want it scaled down to fit the UIImageView
. I tried setting the AspectFit
View mode but the image gets displayed to the original size and is clipped by the UIImageView
. My code is below:
我正在尝试将图像显示为UIImageView
. 我显示的图像很大,我希望它缩小以适合UIImageView
. 我尝试设置AspectFit
查看模式,但图像显示为原始大小并被UIImageView
. 我的代码如下:
- (void)changeImages
{
UIImage* img11 = nil;
img11 = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"jpeg"]];
u11.contentMode = UIViewContentModeScaleAspectFit;
u11.image = img11;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self changeImages];
}
Can anyone shed some light on this please?
任何人都可以对此有所了解吗?
Thanks!
谢谢!
回答by Fogmeister
Hi I would try this...
嗨,我会试试这个...
- (void)changeImages
{
UIImage *img11 = [UIImage imageNamed@"dog.jpeg"];
u11.contentMode = UIViewContentModeScaleAspectFit;
u11.clipsToBounds = YES;
[u11 setImage:img11];
}
- (void)viewWillAppear:animated
{
[super viewWillAppear:animated];
[self changeImages];
}
This will scale the image (up or down) so that it fits inside the imageView. Having clipsToBounds isn't necessary but will stop the image from displaying outside the frame of your imageView.
这将缩放图像(向上或向下),使其适合图像视图。clipsToBounds 不是必需的,但会阻止图像显示在 imageView 的框架之外。
HTH.
哈。
回答by Erhan Demirci
Add to your UIViewController.m:
添加到您的 UIViewController.m:
-(UIImage *)resizeImage:(UIImage *)image imageSize:(CGSize)size
{
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0,0,size.width,size.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
// here is the scaled image which has been changed to the size specified
UIGraphicsEndImageContext();
return newImage;
}
Using:
使用:
UIImage *image = [UIImage imageNamed:@"image.png"];
CGSize size = CGSizeMake(50, 63); // set the width and height
UIImage *resizedImage = [self resizeImage:image imageSize:size];
I hope it helps.
我希望它有帮助。
回答by Abhilash Reddy
CGSize size=CGSizeMake(79, 84);//set the width and height
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0,0,size.width,size.height)];
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
//here is the scaled image which has been changed to the size specified
UIGraphicsEndImageContext();
This works for sure and don't forget to import QuartzCore FrameWork..
这肯定有效,不要忘记导入 QuartzCore FrameWork ..
Have a Happy Coding (^_^)....
祝你编码愉快 (^_^)....
回答by Tad
You can also set View:Mode to Aspect Fit in the Attributes Inspector in Interface Builder
您还可以在 Interface Builder 的 Attributes Inspector 中将 View:Mode 设置为 Aspect Fit
回答by Gulz
I did the following and it helped change the mode to "aspect fill" from the default value "Scale to fill"
我做了以下操作,它帮助将模式从默认值“缩放填充”更改为“纵横比填充”
and add a line of code as follows (I did it in a cell configuration):
并添加如下一行代码(我是在单元格配置中完成的):
cell.photo.clipsToBounds = true
回答by vallard
I know this is old but I wanted to add a response based on the Stanford 193p 2017 lecture 11 (around 18:45) and for anyone looking in swift as this is the first search result that showed up for me.
我知道这是旧的,但我想添加基于斯坦福 193p 2017 讲座 11(大约 18:45)的回复,并且对于任何快速查找的人来说,因为这是出现在我身上的第一个搜索结果。
Basically, subclass UIView and make it look like:
基本上,子类化 UIView 并使其看起来像:
class U11: UIView {
var myImage: UIImage? { didSet { setNeedsDisplay() }}
override func draw(_ rect: CGRect) {
myImage?.draw(in: bounds)
}
}
Then set the image with:
然后设置图像:
func changeImage() {
if let img11 = UIImage(named: "dog.jpeg"){
u11.myImage = img11
}
}
This is super simple and the image takes up the whole space inside of the views bounds.
这非常简单,图像占据了视图边界内的整个空间。