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 的大小
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01