[英]UIImageView's contentmode not respected when setting image during runtime
I've got a UIImageView
which is displayed fullscreen as a 'background'. 我有一个
UIImageView
,它作为“背景”全屏显示。 Whenever I set a image in the viewdidload
as it's default, it the contentMode
works just fine. 每当我在
viewdidload
中将图像设置为默认图像时,它的contentMode
可以正常工作。
However, when I do this: 但是,当我这样做时:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
self.pickedPhotoImageView.contentMode = UIViewContentModeScaleAspectFit; // set desired contentMode here
self.pickedPhotoImageView.clipsToBounds = YES;
self.pickedPhoto = image;
[self.pickedPhotoImageView setImage:image];
[self dismissViewControllerAnimated:YES completion:nil];
}
All respect for the contentMode
is lost and when taking a photo only a tiny bit of the photo is shown. 失去对
contentMode
所有尊重,并且在拍摄照片时仅显示照片的一小部分。 How can a UIImageView lose respect of a property just like that? 像这样,UIImageView如何失去对属性的尊重?
Make sure the UIImageView
has constraints set up properly. 确保
UIImageView
具有正确设置的约束。 With Auto Layout, the size of each view is determined by the view's intrinsicContentSize
(link) . 使用自动版式时,每个视图的大小由视图的
intrinsicContentSize
(链接)确定 。 This means when you reset the content of a subclass of UIView
(wether it's the text in a UILabel
or in this case, the image in UIImageView
, auto layout will calculate the new size. If you don't have the proper constraints set up and set a content size larger than the current frame size, the frame will grow. 这意味着,当您重置
UIView
子类的内容时(无论是UILabel
的文本,还是在这种情况下, UIImageView
的图像,自动布局都会计算出新的大小。如果您没有设置适当的约束,如果将内容的大小设置为大于当前框架的大小,则框架将增大。
As to why this is happening even though you are setting the contentMode
and clipsToBounds
, contentMode
seems to be done after the fact and will work with the constraints of height and width (either the height and width you give it, or the calculated height and width from the relative constraints on X and Y). 至于为什么会发生这种情况,即使您设置了
contentMode
和clipsToBounds
, contentMode
似乎在事后完成,并且将在高度和宽度的约束下工作(无论是您给定的高度和宽度,还是计算出的高度和宽度来自X和Y的相对约束)。 clipsToBounds
works in the same way, where it will clip anything outside of the frame's bounds after the layout has been calculated. clipsToBounds
工作方式相同,在计算布局后,它将裁剪框架边界之外的任何内容。 In this case, the layout was calculated and deemed to be larger than the screen, and set the frame of the UIImageView
accordingly which is why you only saw a tiny bit of the photo. 在这种情况下,计算出的布局被认为大于屏幕,并据此设置
UIImageView
的框架,这就是为什么您只看到一小部分照片的原因。
Hopefully that explanation helps! 希望这种解释有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.