2015-09-13 96 views
0

我使用下面的動畫顯示的縮略圖的全屏圖像到視圖:目標C - 設置ImageView的寬度和保持長寬比

[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{ 

      [testImageView setFrame:CGRectMake(0, 0, testImage.size.width, testImage.size.height)]; 

     }completion:^(BOOL finished){ 

}]; 

然而,這將不包含在範圍內的圖像如果是大圖像,則顯示屏幕。

有沒有一種方法可以將圖像的寬度設置爲屏幕寬度,然後調整圖像的高度,然後保持適當的寬高比?

回答

1

像上面的方法,而是多一點修改:

CGFloat verticalScale = image.height/bounds.height; 
CGFloat horizontalScale = image.width/bounds.width; 
CGFloat scale = max(verticalScale,horizontalScale); 
CGFloat imageViewHeight = image.height/scale; 
CGFloat imageViewWidth = image.width/scale; 
CGRect imageViewFrame = CGRectMake(x: DESIRE_X, y: DESIRE_Y, width:imageViewWidth ,height:imageViewHeight) 
imageView.frame = imageViewFrame; 
imageView.contentMode = UIViewContentModeScaleAspectFill; 
0
CGRect bounds = [UIScreen mainScreen].bounds; 
[testImageView setFrame:bounds]; 
testImageView.contentMode = UIViewContentModeScaleAspectFit; 

但可能會有一些墊襯如果圖像的規模並不等於screen's.If你喜歡覆蓋整個屏幕上的圖像,然後更改contentMode到UIViewContentModeScaleAspectFill。

0

請試試這個下面的代碼:

testImageView 
    =[[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y ,self.view.frame.size.width, self.view.frame.size.height 
    )] 
相關問題