如何按比例缩放UIImageView? [英] How to scale a UIImageView proportionally?

查看:534
本文介绍了如何按比例缩放UIImageView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIImageView,目的是通过给它一个高度或宽度按比例缩放。

I have a UIImageView and the objective is to scale it down proportionally by giving it either a height or width.

UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3092/2915896504_a88b69c9de.jpg"]]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 

//Add image view
[self.view addSubview:imageView];   

//set contentMode to scale aspect to fit
imageView.contentMode = UIViewContentModeScaleAspectFit;

//change width of frame
CGRect frame = imageView.frame;
frame.size.width = 100;
imageView.frame = frame;

图片已调整大小,但位置不在左上角。

The image did get resized but the position is not at the top left. What is the best approach to scaling image/imageView and how do I correct the position?

推荐答案

修复很容易,一旦我找到了文档!

Fixed easily, once I found the documentation!

 imageView.contentMode = UIViewContentModeScaleAspectFit;

这篇关于如何按比例缩放UIImageView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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