• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

调整UIImage的大小并更改UIImageView的大小

用户头像
it1352
帮助1

问题说明

我有这个 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!

正确答案

#1

当你获得调整大小的图片的宽度和高度时获取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
系列文章
更多 icon
同类精品
更多 icon
继续加载