最近产品要求图片缩略图的长度变短可以但是不能被压缩,这主要是针对那些很长的图片来说的。本文中介绍一下是如何满足pm需求的:
CGFloat width = 300.0f;
//get fitSize
UIImage *image = [UIImage imageNamed:@"1"];
CGSize size = CGSizeMake(width, image.size.height/image.size.width * width);
//creat imageview
self.imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, size.width, size.height)];
self.imageView1.contentMode = UIViewContentModeScaleAspectFill;
self.imageView1.clipsToBounds = YES;
self.imageView1.image = image;
[self.view addSubview:self.imageView1];
//specified height
self.imageView1.frame = CGRectMake(0, 0, 300, 100);
其实主要是利用了imageView的contentMode和clipsToBounds的属性。但是在修改时需注意顺序:先将图片贴到与自身比例相当的imageView上面,然后再改变imageView的尺寸。