我尝试将图像放入我的UIImageView
中,称为imgBack
我在标题中将UIImageView
定义为IBOutlet
IBOutlet UIImageView *imgBack;
在我的viewDidLoad
函数中,我将contentMode设置为:
imgBack.contentMode = UIViewContentModeScaleAspectFit;
但在我将图片加载到imgBack之后:
imgBack.image = [UIImage imageWithContentsOfFile: imagePath];
它不会缩放以适合imageview
这是我加载到imgBack的图片: image i try to load
这就是视图中的样子:image in UIImageView
如您所见,图片不适合视图。 有谁知道为什么?
答案 0 :(得分:1)
如果更改ImageView contentMode不适合您,那么您可以尝试使用以下代码调整图像大小以适合图像视图
UIImage *originalImage = [UIImage imageWithContentsOfFile: imagePath];
imgBack.image = [self resizeImage: originalImage imageSize: imgBack.size];
并将此代码添加到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;
}
答案 1 :(得分:0)
首先将图片附加到UIImageView
,然后更改content mode
imgBack.image = [UIImage imageWithContentsOfFile: imagePath];
imgBack.contentMode = UIViewContentModeScaleAspectFill;
imgBack.clipsToBounds = YES;
答案 2 :(得分:-2)
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,