简体   繁体   English

图像不适合UIImageView

[英]image doesn't fit in UIImageView

I am a beginner in iOS development and stuck at the following problem. 我是iOS开发的初学者,但遇到以下问题。

In .xib file I have button and imageview as displayed in following image. 在.xib文件中,我具有按钮和imageview,如下图所示。

在此处输入图片说明

Now I did the following to show image in image view and set content mode in .m file. 现在,我执行以下操作以在图像视图中显示图像并在.m文件中设置内容模式。

UIImage *save = UIGraphicsGetImageFromCurrentImageContext();
self.ivCard.image = save;
self.ivCard.contentMode = UIViewContentModeScaleAspectFit;
self.ivCard.clipsToBounds = YES;

Now the result looks like the following image. 现在,结果如下图所示。

在此处输入图片说明

The original image has resolution of 2000x1000, here it is 原始图像的分辨率为2000x1000,在这里

在此处输入图片说明

The image is automatically cropped from the right side, it should be fit with image view. 图像会自动从右侧裁剪,应适合图像视图。

Pretty sure you didn't add constraints to the ImageView, the ImageView size it's not the same as the device size. 可以肯定的是,您没有向ImageView添加约束,因为ImageView的大小与设备的大小不同。

Add constraints from the ImageView to the borders in the Interface Builder, and it will work. 将约束从ImageView添加到Interface Builder的边框中,它将起作用。

Check: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithConstraints/WorkingwithConstraints.html 检查: https : //developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithConstraints/WorkingwithConstraints.html

There are 2 ways to achieve it. 有两种方法可以实现它。

  1. You can either go for constraints and add constraints to your image view either from your .xib file or prgrammatically as shown below: 您可以从.xib文件中或从语法上如下所示去约束并向图像视图添加约束,如下所示:

    // align ivCard from the left and right //左右对齐ivCard

      [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[ivCard]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(ivCard)]]; 

    // align ivCard from the top and bottom //从顶部和底部对齐ivCard

     [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[ivCard]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(ivCard)]]; 
  2. You can directly set frame for image view using this code: 您可以使用以下代码直接为图像视图设置框架:

     CGFloat btnHeight = 20; // Your back button height from xib self.ivCard.frame = CGRectMake(0, btnHeight, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - btnHeight); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM