2012-12-06 197 views
2

我很喜歡使用UIScrollView縮放縮放功能。但問題是滾動視圖中圖像的方面適合度。UIImageView縮放UIScrollView

目前,我有這個,如下圖:

enter image description here

但我想圖像要適合屏幕像下圖:

enter image description here

和景觀相同的行爲。我怎樣才能做到這一點?

下面是代碼:

- (void)viewDidLoad 
{ 
    UIImage *image = [UIImage imageWithData:appDelegate.selectedOriginalImage]; 
    imgView.image = image; 
    CGSize imageSize = image.size; 
    CGRect rect = CGRectMake(0, 0, imageSize.width, imageSize.height); 
    imgView.frame = rect; 
    scrollView.contentSize = CGSizeMake(imgView.frame.size.width, imgView.frame.size.height); 
    scrollView.maximumZoomScale = 4.0; 
    scrollView.minimumZoomScale = 1.0; 
    scrollView.delegate = self; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    UIImage *image = [UIImage imageWithData:appDelegate.selectedOriginalImage]; 
    CGSize imageSize = image.size; 
    CGRect rect = CGRectMake(0, 0, imageSize.width, imageSize.height); 
    imgView.frame = rect; 

    [scrollView setContentOffset:CGPointMake(0, 0)]; 

    return YES; 
} 
+0

它看起來像你的代碼中的ImageView的大小設置爲圖像的原始大小的imgView。我認爲如果你定義了你想要的圖像視圖的大小,並保持方面適合你應該得到你想要的效果。 – propstm

回答

4

您設置的UIImageView到圖像的原始尺寸,而不是包含它的的UIScrollView的大小。

- (void)viewDidLoad 
{ 
    UIImage *image = [UIImage imageWithData:appDelegate.selectedOriginalImage]; 
    imgView.image = image; 

    imgView.frame = scrollView.bounds; 
    [imgView setContentMode:UIViewContentModeScaleAspectFit]; 
    scrollView.contentSize = CGSizeMake(imgView.frame.size.width, imgView.frame.size.height); 
    scrollView.maximumZoomScale = 4.0; 
    scrollView.minimumZoomScale = 1.0; 
    scrollView.delegate = self; 
} 

記得返回上viewToZoom委託方法

+1

Thanx Pacu。它的工作:) –

+0

不客氣:) – Pacu