2012-07-12 97 views
0

我有一個UIImageView與UIImageView爲了啓用縮放功能。與UIImageView的UIScrollView部分可見在屏幕上

現在的問題是,scrollView應顯示全屏(navigationBar除外),但它顯示了一個奇怪的「偏移量」。

由於該圖片說明超過1000個字,那就是: http://i47.tinypic.com/25pprg3.png

紅色矩形是多數民衆贊成正在顯示怪異的偏移,而圖像應採取整體看法,但事實並非如此。正如你所看到的,它正在被削減。

我試着改變框架,邊界等,但結果相同。

這裏是我的代碼:

- (void)viewDidLoad 
{ [super viewDidLoad]; 

    self.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor]; 

    imageScrollView.bouncesZoom = YES; 
    imageScrollView.delegate = self; 
    imageScrollView.clipsToBounds = NO; // If set to yes, the image would be like the screen above 
    imageView = [[UIImageView alloc] init]; 
    imageView.clipsToBounds = YES; 


    imageScrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin); 

    //activity indicator 
    UIActivityIndicatorView *activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease]; 
    activityIndicator.hidesWhenStopped = YES; 
    activityIndicator.hidden = NO; 
    activityIndicator.center = CGPointMake(self.imageView.frame.size.width /2, self.imageView.frame.size.height/2); 
    [activityIndicator startAnimating]; 


    [imageView setImageWithURL:[NSURL URLWithString:tipImageString] 
       placeholderImage:nil options:SDWebImageProgressiveDownload 
         success:^(UIImage *image) { [activityIndicator stopAnimating];[activityIndicator removeFromSuperview]; } 
         failure:^(NSError *error) { [activityIndicator stopAnimating];[activityIndicator removeFromSuperview]; }]; 
    [imageView addSubview:activityIndicator]; 

    imageView.userInteractionEnabled = YES; 
    imageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin); 
    imageView.center = [[imageScrollView window] center]; 
    imageView.contentMode = UIViewContentModeScaleAspectFit; 
    [imageScrollView setContentMode:UIViewContentModeScaleAspectFit]; 
    [imageScrollView addSubview:imageView]; 
    imageScrollView.contentSize = self.imageView.image.size;  
    imageScrollView.decelerationRate = UIScrollViewDecelerationRateFast; 

    CGSize boundsSize = self.imageScrollView.bounds.size; 
    imageView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); 
    CGRect frameToCenter = imageView.frame; 

    // center horizontally 
    if (frameToCenter.size.width < boundsSize.width) 
     frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width)/2; 
    else 
     frameToCenter.origin.x = 0; 

    // center vertically 
    if (frameToCenter.size.height < boundsSize.height) 
     frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height)/2; 
    else 
     frameToCenter.origin.y = 0; 

    imageView.frame = frameToCenter; 

    // calculate minimum scale to perfectly fit image width, and begin at that scale 
    float minimumScale = 0.50;//[imageScrollView frame].size.width/[imageView frame].size.width; 
    imageScrollView.maximumZoomScale = 5.0; 
    imageScrollView.minimumZoomScale = minimumScale; 
    imageScrollView.zoomScale = minimumScale; 
    [imageScrollView setContentMode:UIViewContentModeScaleAspectFit]; 
    [imageView sizeToFit]; 

    [imageScrollView setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)]; 

} 

我什麼都試過,但沒有成功!

我認爲這是與框架的東西,但我無法弄清楚什麼。

任何幫助讚賞

回答

2

@Pheel很高興你發現它是由於CGAffineTransformMakeRotation的形象。這可能會改變座標。

+0

)。我不得不再次設置imageScrollView框架! – Phillip 2012-07-13 15:16:11

0

嘗試更改圖像分辨率。另外,你是否嘗試在視覺上放置滾動視圖,然後將圖像視圖放置在Interface Builder中。這樣做之後,你可以在方法初始化如viewDidLoad中

+0

嘗試了幾個圖像(甚至更低的分辨率),相同的結果。是的,一切都以IB連接,沒有增強功能。 – Phillip 2012-07-12 21:55:59

+0

屬性檢查器怎麼樣? – 2012-07-12 21:58:17

+0

一切都很好,scrollView被設置爲全屏顯示(整個視圖) – Phillip 2012-07-12 22:03:23

0

爲什麼你不只是做:

... set-up imageView... 
imageView.frame = imageScrollView.bounds; 
imageView.contentMode = UIViewContentModeScaleAspectFit; 
[imageScrollView addSubview:imageView]; 

你有很多的代碼是多餘的,或者在你的方法累積效應( frame/center,contentMode,contentSize ...)的多次連續變化,這使得很難知道是什麼導致了什麼(可能是滾動視圖剪切圖像視圖,其中心首先基於窗口座標而不是其超視圖座標,但最後不會,因爲你重新定義了它的框架......)。你應該從頭開始,用一些簡單的代碼,具體取決於你想要什麼,以及你開始的配置(例如什麼是滾動視圖框?)。

+0

那麼我從我的其他項目的代碼,它完美的工作。我不知道爲什麼現在它不起作用(或者更好,但它不像舊項目中那樣)。我會盡力去做你所做的事情,但是代碼有一些調整,使得圖像處於視圖的完美中心(即 – Phillip 2012-07-12 22:28:47

相關問題