Resize UIImage and change the size of UIImageView(调整 UIImage 大小并更改 UIImageView 的大小)
问题描述
我有这个 UIImageView 并且我有它的最大高度和最大宽度的值.我想要实现的是我想要拍摄图像(具有任何宽高比和任何分辨率)并且我希望它适合边框,所以图片不超过它们,但它可以根据需要缩小它们.(图中红色标注):
I have this UIImageView and I have the values of its max height and max width. What I want to achieve is that I want to take the image (with any aspect ratio and any resolution) and I want it to fit in the borders, so the picture does not exceed them, but it can shrink them as it wants. (marked red in the picture):
现在图像适合必要的尺寸,但我有两个担心:1. UIImageView 不等于调整后图像的大小,因此留下红色背景(我不希望这样)2. 如果图像小于我的 UIImageView 的高度,则它没有调整为更小,它保持相同的高度.
Right now the image fits the necessary size properly, but I have 2 worries:
1. The UIImageView is not equal the size of the resized image, thus leaving red background (and I don't want that)
2. If the image is smaller that the height of my UIImageView it is not resized to be smaller, it stays the same height.
这是我的代码,我知道它错了:
Here's my code and I know its wrong:
UIImage *actualImage = [attachmentsArray lastObject];
UIImageView *attachmentImageNew = [[UIImageView alloc] initWithFrame:CGRectMake(5.5, 6.5, 245, 134)];
attachmentImageNew.image = actualImage;
attachmentImageNew.backgroundColor = [UIColor redColor];
attachmentImageNew.contentMode = UIViewContentModeScaleAspectFit;
那么,我如何动态更改UIImageView.image 的大小,以及整个UIImageView 的大小,从而使其大小完全可以根据内容调整.任何帮助将不胜感激,谢谢!
So how do I dynamically change the size not only of the UIImageView.image, but of the whole UIImageView, thus making its size totally adjustable to its content. Any help would be much appreciated, thanks!
推荐答案
当你得到一个调整大小的图片的宽高时 获取UIViewContentModeScaleAspectFit后调整后图片的宽度,可以调整imageView的大小:
When you get the width and height of a resized image Get width of a resized image after UIViewContentModeScaleAspectFit, you can resize your imageView:
imageView.frame = CGRectMake(0, 0, resizedWidth, resizedHeight);
imageView.center = imageView.superview.center;
我没有检查它是否有效,但我认为一切都应该没问题
I haven't checked if it works, but I think all should be OK
这篇关于调整 UIImage 大小并更改 UIImageView 的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:调整 UIImage 大小并更改 UIImageView 的大小
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
