简体   繁体   English

在运行时设置图像时不遵守UIImageView的contentmode

[英]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). 至于为什么会发生这种情况,即使您设置了contentModeclipsToBoundscontentMode似乎在事后完成,并且将在高度和宽度的约束下工作(无论是您给定的高度和宽度,还是计算出的高度和宽度来自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.

相关问题 设置UIImageView contentMode无效 - Setting UIImageView contentMode shows no effect 在运行时使用图像设置UIImageView会导致其不遵守约束 - Setting UIImageView with an image during runtime causes it to not respect constraints UICollectionViewCell UIImageView 忽略 contentMode,溢出图像 - UICollectionViewCell UIImageView ignores contentMode, overflows image 当contentMode UIViewContentModeScaleAspectFit时,禁用UIImageView调整大小的行为 - Disable UIImageView resizing behavior when contentMode UIViewContentModeScaleAspectFit 如何避免使用UIImageView的contentmode UIViewContentModeScaleAspectFit保持高度恒定而损失图像质量? - How to avoid losing image quality using UIImageView's contentmode UIViewContentModeScaleAspectFit keeping height constant? 设置图像时,UIImageView的属性和约束会重置 - UIImageView's properties and constraints get reset when setting image 何时/如何设置UIImageView contentMode(UIImageView setContentMode不起作用) - When/How to set UIImageView contentMode (UIImageView setContentMode not working) 将点从图像转换为UIImageView点,contentMode-aware - Convert points from image to UIImageView points, contentMode-aware UIImageView contentMode Scale Aspect Fill 正在拉伸图像(没关系,它不是) - UIImageView contentMode Scale Aspect Fill is Stretching the Image (Nevermind, It Wasn't) UIImageView框架是正确的,contentMode是正确的,图像无法正确显示 - UIImageView frame is right, contentMode is right, image not displayed correctly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM