2014-02-13 55 views
1

我在一個項目(iOS7 & ARC),其中,我想顯示已存入沙箱目錄中的滾動view.These圖像的圖像的N多工作。我的應用程序我現在面臨一個問題,即滾動型不光滑只有橫向,它堅持2-3次滾動滾動視圖並不順利

這是我如何配置ScrollView

[self.containerScroll setAutoresizesSubviews:NO]; 
self.containerScroll.pagingEnabled = YES; 
self.containerScroll.showsHorizontalScrollIndicator = NO; 
self.containerScroll.showsVerticalScrollIndicator = NO; 
self.containerScroll.scrollsToTop = NO; 
self.containerScroll.maximumZoomScale = 5.0; 
self.containerScroll.minimumZoomScale = 1.0; 
self.containerScroll.delegate = self; 

我一次只保留三個圖像scrollView

我加載在ScrollView圖片如下方法

-(void) loadScrollViewWithPage:(int) page{ 

if (page >= self.numberOfSlides) 
    return; 

float image_width; 
float image_height; 

if(self.isFromListView){ 

    if(IS_IPHONE5){ 
     image_width = 568.0f; 
     image_height = 320.0f; 
    } else{ 
     // iPhone retina-3.5 inch 
     image_width = 480.0f; 
     image_height = 320.0f; 
    } 

} 
else{ 
    image_width = IMAGE_WIDTH; 
    image_height = IMAGE_HEIGHT; 
} 

CGFloat xPos = page * image_width; 
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(xPos, 0.0f, image_width, image_height)]; 
imgView.tag = page; 
NSString *imgPath = [self.storageDirPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%d%@", page, Image_Extension_JPG]]; 

NSFileManager *fileManager = [NSFileManager defaultManager]; 
__block UIImage *img = nil; 
if(![fileManager fileExistsAtPath:imgPath]){ 
    [imgView setContentMode:UIViewContentModeCenter]; 
    img = [UIImage imageNamed:@"icon-loader.png"]; 
    [imgView setImage:img]; 
} 
else{ 
    [imgView setContentMode:UIViewContentModeScaleAspectFit]; 
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ 

       img = [[UIImage alloc] initWithCGImage:[[UIImage imageWithData:[NSData dataWithContentsOfFile:imgPath]] CGImage] scale:1.0 orientation:UIImageOrientationUp]; 

         dispatch_async(dispatch_get_main_queue(), ^{ 
         [imgView setImage:img]; 
       }); 

      }); 
} 

[self.containerScroll addSubview:imgView]; 
img = nil; 
fileManager = nil; 
imgView = nil; 

}

,這我ScrollView委託方法如何去...

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{ 
    self.containerScroll.scrollEnabled = YES; 

    float page = self.containerScroll.contentOffset.x/self.view.frame.size.width; 
    showingSlide = (UInt16) roundf(page); 

    if(scrollView == self.containerScroll){ 

     // switch the indicator when more than 50% of the previous/next page is visible 
     CGFloat pageWidth = CGRectGetWidth(self.containerScroll.frame); 
     NSUInteger pageNo = floor((self.containerScroll.contentOffset.x - pageWidth/2)/pageWidth) + 1; 

     // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling) 
     [self loadScrollViewWithPage:pageNo - 1]; 
     [self loadScrollViewWithPage:pageNo]; 
     [self loadScrollViewWithPage:pageNo + 1]; 

     // a possible optimization would be to unload the views+controllers which are no longer visible 

     if(scrollView == self.containerScroll) 
     { 
      [self.previewTableView reloadData]; 
      [self.previewTableView setContentOffset:CGPointMake(0, (page*220)+64) animated:NO]; 
      [self.previewTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:page inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; 

      [self updateSlideNumber]; 
      [self flashSlideNumber]; 
     } 

     //unload unnecessary imageviews from scroll view 
     for (UIView* view in self.containerScroll.subviews) { 
      if ([view isKindOfClass:[UIImageView class]] && view.tag != page && view.tag != page-1 && view.tag != page+1) { 
       [view removeFromSuperview]; 
      } 
     } 

    } 
} 

現在的問題是平滑scrollView。當我開始滾動時,它滾動得很好,但在2或3(或任何隨機數之後)頁面滾動後,它會卡住,嘗試2-3次後,只會再次移動,我不得不輕輕滑動。提前致謝。

回答

0

我認爲這是一個內存問題在你的代碼中嘗試@autorelease池。

+0

我已經試過這種方式...沒有運氣。 – Suryakant

0

使用scrollview不是一個好的方法來顯示圖像,我會建議你要麼使用tableview或collevtionview相同。

因爲scollview不會重複使用內存,所以應用程序的內存將隨着每次滾動而不斷增加,另一方面,tableview和collectionview會重用內存。

+0

你和@HookShot是正確的..應該使用TableView或集合視圖,但在這個階段的項目,我不能這樣做...必須去與ScrollView。 – Suryakant

0

作爲改善效果的最有效方法,在您監視應用程序中的內存使用情況時,確實很慢(一次一個)滾動。當每個新圖像添加到視圖中時,您都可以觀看它,特別是如果您沒有對圖像進行任何優化。

另一件事是,雖然你的代碼看起來像是釋放圖像,但你仍然需要記住,它仍然需要在你滾動時重新加載圖像。你正在主線程上創建你的圖像,所以你永遠不會得到UITableView的平滑性。雖然我意識到您正在異步線程上創建圖像視圖,但添加和滾動它們的操作仍由主線程處理。

我會建議一個UITableView來解決你的問題,或UICollectionView。如果你使用scrollview,我會建議使用某種類型的破碎機,以儘可能小的圖像尺寸,同時保持質量不錯。

如果您需要關於TableView實現方面的幫助,您應該找到圍繞SO的大量信息。如果你仍然希望它看起來像一個滾動視圖,可能是一個不錯的選擇,只是使所有的分隔符,頭文件等零,然後使用延遲加載的圖像。

+0

一旦圖像添加到ImageView我只是將它設置爲零。它的問題是什麼? – Suryakant

+0

我認爲問題出現在圖像的實際讀取時間內,因爲它們被加載到滾動中。你可以檢查儀器,因爲他們被加載,看看花費的時間來執行負載?我建議使用破碎機作爲您的第一步 - 這可以解決您的所有問題,甚至不必更改代碼太多。是否有可能已經聲明瞭所有的UIImage,並且在滾動時將它們分配給UIImageView,以便圖像已經存儲在內存中,準備裝入UIImageView的容器中? – crashlog

+0

http://imageoptim.com/ - 這是一個很好的imagecrusher的鏈接我用戶的Xcode/Webstuff =] – crashlog

0

你犯了兩個錯誤。起初:從不使用imageNamed作爲非圖形內容(例如,使用imageNamed作爲按鈕背景)。第二:你嘗試實時加載一個大的圖像。所以你滾動視圖有滯後因此。如果您在顯示滾動視圖之前加載所有圖像,則會出現胺化結束滯後現象。但是你可以得到記憶警告。所以,你需要優化它。附:對不起,我的英語

+0

你寫了該死的英文很好..所以不必對不起 第一:imageNamed方法來圖片時真實圖像不存在,所以讓我們不討論這種情況。當imageNamed甚至沒有獲得單次調用時,我的ScrollView滯後。所以這不是問題。 第二:我不能將所有圖像加載到內存中,因爲我可以有任意數量的圖像。 – Suryakant