2012-04-19 10 views
3

UIScrollViewですべての画像を一度に読み込みますが、それは悪い方法なので、最適化する方法はありますか?多数の画像でUIScrollViewを最適化する方法

+0

すべての画像はリモートサーバーから送信されますか? –

+0

画像はすべて同じですか、または異なっていますか?それらが同じであれば 'WWDC 2011セッション104 - 高度なスクロールビューテクニック' – pawelropa

+0

@ AbdullahMd.Zubairいいえ、ローカルイメージ – Mil0R3

答えて

3
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { 
    int currentPage = (scrollView.contentOffset.x/scrollView.frame.size.width); 

    // display the image and maybe +/-1 for a smoother scrolling 
    // but be sure to check if the image already exists, you can 
    // do this very easily using tags 
    if ([scrollView viewWithTag:(currentPage + 1)]) { 
     return; 
    } else { 
     // view is missing, create it and set its tag to currentPage+1 
     UIImageView *iv = [[UIImageView alloc] initWithFrame: 
      CGRectMake((currentPage + 1) * scrollView.frame.size.width, 
         0, 
         scrollView.frame.size.width, 
         scrollView.frame.size.height)]; 
     iv.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpg", 
                    currentPage + 1]]; 
     iv.tag = currentPage + 1; 
     [sv addSubview:iv]; 
    } 

    /** 
    * using your paging numbers as tag, you can also clean the UIScrollView 
    * from no longer needed views to get your memory back 
    * remove all image views except -1 and +1 of the currently drawn page 
    */ 
    for (int i = 0; i < 50; i++) { 
     if ((i < (currentPage - 1) || i > (currentPage + 1)) && 
      [scrollView viewWithTag:(i + 1)]) { 
      [[scrollView viewWithTag:(i + 1)] removeFromSuperview]; 
     } 
    } 
} 
1

thisチュートリアルを使用して手助けすることができます。私はuser1212112が言ったこともお勧めしますが、時計はWWDC 2011 Session 104 - Advanced Scroll View Techniquesです。

+0

ありがとう、私はチュートリアルを流してみました、レイジーローディングは本当に良い方法ですが、私はUIScrollViewを速くスワップすると、レイジーローディングイメージは表示されませんでした。 – Mil0R3

0

VSScrollview VSScrollviewをご覧ください。それはあなたがそれに渡すその見解を再利用します。その使用はUITableviewを使用するのと同じくらい簡単です。 datasoure方法以下の実装

-(VSScrollViewCell *)vsscrollView:(VSScrollView *)scrollView viewAtPosition:(int)position; 
// implement this to tell VSScrollview the view at position "position" . This view is VSScrollviewCell or subclass of VSScrollviewCell. 

-(NSUInteger)numberOfViewInvsscrollview:(VSScrollView *)scrollview; 

// implement this to tell VSScrollview about number of views you want in VSSCrollview. 

あなたはあなたが異なる幅、高さ、間隔及び各ビューのscrollviewのも、コンテンツサイズを有することができるVSScrollviewの動作をカスタマイズすることができ、あまりにもそれによって他の任意のデータソースとデリゲートメソッドがあります。

関連する問題