简体   繁体   English

UIImageView 中的 UIViewContentModeScaleAspectFit 无法正常工作

[英]UIViewContentModeScaleAspectFit in UIImageView does not work correctly

I try to fit an Image into my UIImageView which is called imgBack I define my UIImageView in the header as IBOutlet我尝试将一个图像放入我的UIImageView ,它被称为 imgBack 我在标题中将我的UIImageView定义为IBOutlet

IBOutlet UIImageView *imgBack;

in my viewDidLoad function I set the contentMode like this:在我的viewDidLoad函数中,我像这样设置 contentMode:

imgBack.contentMode = UIViewContentModeScaleAspectFit;

But after I load an image into imgBack like this:但是在我像这样将图像加载到 imgBack 之后:

imgBack.image = [UIImage imageWithContentsOfFile: imagePath];

it does not scale to fit into the imageview它不会缩放以适应图像视图

This is the picture I load into imgBack: image i try to load这是我加载到 imgBack 中的图片:我尝试加载的图片

And this is how it looks like in the view: image in UIImageView这就是它在视图中的样子: UIImageView 中的图像

As you see, the image does not fit into the View.如您所见,图像不适合视图。 Does anyone know why?有谁知道为什么?

If changing ImageView contentMode does not work for you, then you can try to resize the image to fit in Image View with help of following code如果更改 ImageView contentMode 对您不起作用,那么您可以尝试在以下代码的帮助下调整图像大小以适合 Image View

UIImage *originalImage = [UIImage imageWithContentsOfFile: imagePath];
imgBack.image = [self resizeImage: originalImage imageSize: imgBack.size];

and add this code to your ViewController并将此代码添加到您的 ViewController

-(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;

    }

Attach the image to UIImageView first and then change the content mode先将图片附加到UIImageView再更改content mode

imgBack.image = [UIImage imageWithContentsOfFile: imagePath];
imgBack.contentMode = UIViewContentModeScaleAspectFill;
imgBack.clipsToBounds = YES;
below are the mode try and check
UIViewContentModeScaleToFill,
    UIViewContentModeScaleAspectFit,      // contents scaled to fit with fixed aspect. remainder is transparent
    UIViewContentModeScaleAspectFill,     // contents scaled to fill with fixed aspect. some portion of content may be clipped.
    UIViewContentModeRedraw,              // redraw on bounds change (calls -setNeedsDisplay)
    UIViewContentModeCenter,              // contents remain same size. positioned adjusted.
    UIViewContentModeTop,
    UIViewContentModeBottom,
    UIViewContentModeLeft,
    UIViewContentModeRight,
    UIViewContentModeTopLeft,
    UIViewContentModeTopRight,
    UIViewContentModeBottomLeft,
    UIViewContentModeBottomRight,

暂无
暂无

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

相关问题 UIViewContentModeScaleAspectFit不适用于UIScrollView子视图UIImageView - UIViewContentModeScaleAspectFit not working for UIScrollView subview UIImageView 具有contentMode UIViewContentModeScaleAspectFit的UIImageView的UILabel - UILabel for UIImageView that has contentMode UIViewContentModeScaleAspectFit 使用UIViewContentModeScaleAspectFit在UIImageView中调整图像 - Fit image in UIImageView using UIViewContentModeScaleAspectFit 为什么UIViewContentModeScaleScaleAspectFit在UIScrollView内的UIImageView中不起作用? 见所附图片 - Why doesn't UIViewContentModeScaleAspectFit work in a UIImageView inside UIScrollView? See Image attached UIViewContentModeScaleAspectFit不起作用 - UIViewContentModeScaleAspectFit doesn't work 当contentMode UIViewContentModeScaleAspectFit时,禁用UIImageView调整大小的行为 - Disable UIImageView resizing behavior when contentMode UIViewContentModeScaleAspectFit UIScrollView中的UIImageView无法正确缩放 - UIImageView in UIScrollView does not zoom correctly UIViewContentModeScaleAspectFit始终将UIImageView垂直或水平居中。 可以吗 - UIViewContentModeScaleAspectFit always centers the UIImageView, either vertically or horizontally. Can it not? 在将contentMode设置为UIViewContentModeScaleAspectFit时,如何设置UIImageView左对齐或右对齐? - how to set the UIImageView align left or right when set the contentMode to UIViewContentModeScaleAspectFit? UIImageView setImage不适用于IBOutlet - UIImageView setImage does not work for IBOutlet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM