调整UIImage的大小并更改UIImageView的大小 [英] Resize UIImage and change the size of UIImageView

查看:508
本文介绍了调整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

这篇关于调整UIImage的大小并更改UIImageView的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆