调整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):
现在图片的大小合适,但我有2个担忧:
1. UIImageView
不等于调整大小的图像的大小,因此留下红色背景(而我不想那样)
2.如果图像小于我的
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;
我没有检查它是否有效,但我认为所有都应该OK了
I haven't checked if it works, but I think all should be OK
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhchaeje
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20