简体   繁体   English

调整图像大小以适合UIImageView

[英]Resizing image to fit UIImageView

I am very new to objective c and I'm just getting my bearings. 我对目标c还是很陌生,但我的目标才刚刚开始。 I want to do something really simple but it proves to be quite a challenge: 我想做一些非常简单的事情,但是事实证明这是一个很大的挑战:

I am trying to display an image into an UIImageView . 我正在尝试将图像显示到UIImageView The image I'm showing is large and I want it scaled down to fit the UIImageView . 我显示的图像很大,我希望将其缩小以适合UIImageView I tried setting the AspectFit View mode but the image gets displayed to the original size and is clipped by the UIImageView . 我尝试设置AspectFit View模式,但是图像显示为原始大小,并被UIImageView裁剪。 My code is below: 我的代码如下:

- (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! 谢谢!

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. 这将缩放图像(向上或向下),使其适合imageView内。 Having clipsToBounds isn't necessary but will stop the image from displaying outside the frame of your imageView. 不需要clipsToBounds,但可以阻止图像显示在imageView框架之外。

HTH. HTH。

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. 希望对您有所帮助。

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 (^_^).... 祝您编码愉快(^ _ ^)....

您还可以在Interface Builder中的“属性”检查器中将View:Mode设置为Aspect Fit。

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

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. 我知道这很老了,但我想根据Stanford 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. 这非常简单,图像占据了视图边界内的整个空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM