2016-02-05 225 views
0

我的UIImageView具有恆定的寬度*高度是UIView 100 * 100,但我想在這個UIImageView中顯示的圖像是25 * 25(原始尺寸)。在UIImageView中設置圖像與原始尺寸在ios

我不想拉伸圖像。我嘗試UIImageView ContentMode屬性,但沒有工作。

這我不想要.tried sizeToFit和aspectCenter模式。

enter image description here

enter image description here

我需要下面的結果,對於小圖像

enter image description here

如何做到這一點(有,伸出原始圖像尺寸和恆定的UIImageView尺寸)。什麼會如果圖像大於UIImageView?提前致謝。

+0

您使用過[yourImage sizeToFit]嗎? – Hima

+0

嗯@Hima不工作。 –

+0

uiimageview是100 * 100,圖像是25 * 25的場景是什麼? – Hima

回答

2

試試這個 (Objective-C的):

imageView.contentMode = UIViewContentModeCenter; 

(SWIFT):

imageView.contentMode = UIViewContentMode.Center; 
+0

不適用於較大圖像大小。恩。 300 * 300不顯示完整圖像。 –

+0

你使用自動佈局嗎?如果是這種情況,請從圖像中移除大小限制。 –

0

你應該試試這個

imageView.contentMode = UIViewContentModeScaleAspectFit; 

無論圖像小或大,它會縮放圖像。

,如果你不希望比你要調整UIImageView圖像尺寸

您可以從UIImage的獲取圖像尺寸拉伸圖像。 適用於例如

UIImage *image = [UIImage imageNamed:@"imageName"]; 
float imgHeight = image.size.height; 
float imgWidth = image.size.width;