[英]How do I make a UIImageView not be wider than the view it's contained in?
I set the image of a UIImageView to an image that is 1024x1024, and as a result a lot of the image is not visible, especially width wise, and cut off the edges of the screen. 我将UIImageView的图像设置为1024x1024的图像,结果很多图像不可见,尤其是宽度方向不可见,并切断了屏幕的边缘。
I tried using clipsToBounds
: 我尝试使用clipsToBounds
:
UIImageView *imageFromLink = [[UIImageView alloc] initWithImage:responseObject];
imageFromLink.clipsToBounds = YES;
[darkOverlayView addSubview:imageFromLink];
But it doesn't seem to do anything, and the image is still too big for the view. 但是它似乎无能为力,并且图像对于视图来说仍然太大。
You're going to want to change the contentMode
property for the desired effect. 您将要更改contentMode
属性以获得所需的效果。 UIViewContentModeScaleAspectFill
is most likely what you'll want. UIViewContentModeScaleAspectFill
最有可能是您想要的。
You have the set the image view's frame to the size you want. 您已将图像视图的框架设置为所需的大小。 By default it will be the size of the image. 默认情况下,它将是图像的大小。
UIImageView *imageFromLink = [[UIImageView alloc] initWithImage:responseObject];
imageFromLink.contentMode = UIViewContentModeScaleAspectFit;
imageFromLink.frame = CGRectMake(0, 0, desiredWidth, desiredHeight);
[darkOverlayView addSubview:imageFromLink];
Also set the contentMode
so the image is scaled properly in the smaller image view. 还要设置contentMode
以便在较小的图像视图中正确缩放图像。
You should set the imageview's frame equal to the container's bounds. 您应该将imageview的框架设置为等于容器的边界。
UIImageView *imageFromLink = [[UIImageView alloc]initWithImage:responseObject];
imageFromLink.contentMode = UIViewContentModeScaleAspectFit;
imageFromLink.frame = darkOverlayView.bounds;
[darkOverlayView addSubview:imageFromLink];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.