[英]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.