[英]UIImage should not stretched inside the UIImageView
I am using the UIImageView
in my xib. 我在xib中使用UIImageView
。 App is designed for multi device without auto layout. 该应用程序专为无需自动布局的多设备而设计。 It means I am using autoresizing.What I want to do is, only my UIImageView
should autoresize not my Image inside the UIImageView
, but unfortunately my image also getting stretched with UIImageView
. 这意味着我正在使用自动调整大小。我想做的是,只有UIImageView
不能自动调整UIImageView
内的Image的大小,但是不幸的是,我的图像也可以通过UIImageView
拉伸。 I have tried the different ways but could not get success. 我尝试了不同的方法,但没有成功。 Changed the content mode also but didn't work for me. 也更改了内容模式,但对我不起作用。
Try this... 尝试这个...
//set content mode
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
//clear background color
self.imageView.backgroundColor=[UIColor clearColor];
UIViewContentMode 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,
Don't add the UIImage
to the UIImageView
whose size is going to change. 不要将UIImage
添加到要更改其大小的UIImageView
。 Add it to another UIImageView
and make this second UIImageView
a subview
of the first UIImageView
. 将其添加到另一个UIImageView
,并使第二个UIImageView
成为第一个UIImageView
的subview
。
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];
I hope this helps. 我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.