繁体   English   中英

UIImage不应在UIImageView内拉伸

[英]UIImage should not stretched inside the UIImageView

图像尺寸为480X360

我在xib中使用UIImageView 该应用程序专为无需自动布局的多设备而设计。 这意味着我正在使用自动调整大小。我想做的是,只有UIImageView不能自动调整UIImageView内的Image的大小,但是不幸的是,我的图像也可以通过UIImageView拉伸。 我尝试了不同的方法,但没有成功。 也更改了内容模式,但对我不起作用。

尝试这个...

//set content mode
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
//clear background color
self.imageView.backgroundColor=[UIColor clearColor];

UIViewContentMode

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,

不要将UIImage添加到要更改其大小的UIImageView 将其添加到另一个UIImageView ,并使第二个UIImageView成为第一个UIImageViewsubview

UIImage *exampleImage = [UIImage imageWithContentsOfFile:filePath];
UIImageView *variableImageView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

CGFloat imageX = (variableImageView.bounds.size.width - exampleImage.size.width) / 2;
CGFLoat imageY = (variableImageView.bounds.size.height - exampleImage.size.height) / 2;

UIImageView *helperImageView = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, exampleImage.size.width, exampleImage.size.height)];

helperImageView.image = exampleImage;
helperImageView.contentMode = UIViewContentModeScaleAspectFit;

[self.view addSubview:variableImageView];
[variableImageView addSubview:helperImageView];

我希望这有帮助。

暂无
暂无

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

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