简体   繁体   English

根据高度调整UIImageView宽度

[英]Adjust UIImageView width according to height

I have hundreds of images in pdf format to use in my app. 我有数百个pdf格式的图像可以在我的应用程序中使用。 I am using an UIImage-PDF( https://github.com/mindbrix/UIImage-PDF ) category to show pdfs as images in my app. 我正在使用UIImage-PDF( https://github.com/mindbrix/UIImage-PDF )类别在我的应用程序中将pdf显示为图像。 I am using this code 我正在使用此代码

imageView = [[ UIImageView alloc ] initWithFrame:CGRectMake(100, 200, 100, 100)]; 

to create an imageView and then adding image like that 创建一个imageView,然后添加这样的图像

[imageView setImage:[ UIImage imageWithPDFNamed:[NSString stringWithFormat:@"%@.pdf",imageName] atSize:CGSizeMake( 100, 100) ]];

Its working fine for square images, the problem is when width is greater than height. 它适用于方形图像,问题是当宽度大于高度时。 Images are shrunk. 图像缩小了。 What I want is, it should adjust image's width according to height. 我想要的是,它应该根据高度调整图像的宽度。 How can I do that? 我怎样才能做到这一点?

将Imageview的contentMode设置为UIViewContentModeScaleAspectFit

imageView.contentMode = UIViewContentModeScaleAspectFit;

You need to use 你需要使用

imageView.contentMode = UIViewContentModeScaleAspectFit;

Or 要么

imageView.contentMode = UIControlContentHorizontalAlignmentFill

try this, 尝试这个,

UIImage *image =[ UIImage imageWithPDFNamed:[[NSString stringWithFormat:@"%@.pdf",imageName]];
[imageView setImage:image atSize:CGSizeMake( img.size.width, img.size.height) ]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM