簡體   English   中英

根據高度調整UIImageView寬度

[英]Adjust UIImageView width according to height

我有數百個pdf格式的圖像可以在我的應用程序中使用。 我正在使用UIImage-PDF( https://github.com/mindbrix/UIImage-PDF )類別在我的應用程序中將pdf顯示為圖像。 我正在使用此代碼

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

創建一個imageView,然后添加這樣的圖像

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

它適用於方形圖像,問題是當寬度大於高度時。 圖像縮小了。 我想要的是,它應該根據高度調整圖像的寬度。 我怎樣才能做到這一點?

將Imageview的contentMode設置為UIViewContentModeScaleAspectFit

imageView.contentMode = UIViewContentModeScaleAspectFit;

你需要使用

imageView.contentMode = UIViewContentModeScaleAspectFit;

要么

imageView.contentMode = UIControlContentHorizontalAlignmentFill

嘗試這個,

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