简体   繁体   English

如何使UIImageView的宽度不比其包含的视图宽?

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

相关问题 视图正在使用包含 UIImageView 的高度,我该如何调整它的大小? - View is using height of contained UIImageView, how do I get it to resize? 如何通过其Label属性引用视图(UIImageView)? - How do I reference a view (UIImageView) by it's Label property? 如何同时为UIImageView的图像和UIImageView本身设置动画? - How do I simultaneously animate a UIImageView's image and the UIImageView itself? 如何使UIImageView填充UITableViewCell? - How do I make a UIImageView fill a UITableViewCell? 如何使UIImageView“可滑动”? - How do I make a UIImageView “flickable”? 使用自动布局,如何根据图像动态调整 UIImageView 的大小? - With Auto Layout, how do I make a UIImageView's size dynamic depending on the image? 如何在Storyboard / Interface Builder中使UIImageView成为UICollectionViewCell高度的一半 - How do I make a UIImageView be half the height of the UICollectionViewCell's height in Storyboard/Interface Builder 如何使UIImageView可“滑动”并检测其滑动方式? - How do I make a UIImageView “swipable” and detect which way it's swiped? 如何在select上的UICollectionViewCell中包含的UIImageView周围绘制边框? - How do I draw a border around a UIImageView contained within a UICollectionViewCell on select? 如何从视图控制器中包含的视图中调用方法? - How Do I call a method from a view contained in a view controller ?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM