UIScrollview + UIimageviews memory crash

Discussion in 'iOS Programming' started by igorladessa, Dec 10, 2012.

  1. igorladessa macrumors newbie

    Joined:
    Dec 10, 2012
    #1
    I need to scroll over 200 images. I ahve found a tutorial that says that I can over 1000 images with the tutorial code. The tutorial is:

    http://soulwithmobiletechnology.blogspot.com.br/2011/05/how-to-load-1000s-of-uiimageview-on.html?m=1

    I followed the tutorial, but when I use 100 images aprox..the app crash with memory warning.

    My code is here:

    http://pastebin.com/BKmsdXxf

    I have already with UIIMagenamed, with initWithContentsOfFile

    but the problem persist. Pls , help me!
     
  2. Ides macrumors member

    Joined:
    Mar 27, 2012
    #2
    Well how big are the images you're loading? If they are very large then they will cause memory warnings, if you're loading 100+.
     
  3. igorladessa thread starter macrumors newbie

    Joined:
    Dec 10, 2012
    #3

    1,5 mb
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    Use a tableview if you can. Otherwise set things up so that only a small number of image views exist at a time. Unload the image views when they scroll off.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Those are quite large. You might want to consider creating thumbnail versions for display most of the time.

    Yeah, UICollectionView (iOS 6) might be a good solution, if UITableView is not, because it works similar in that cells are reused.
     
  6. igorladessa, Dec 12, 2012
    Last edited by a moderator: Dec 12, 2012

    igorladessa thread starter macrumors newbie

    Joined:
    Dec 10, 2012
    #6
    solved!!

    I release all ImageViews and load the current and next imageView, when scrolling!


    Code:
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
    CGPoint scrollOffset=scrollView.contentOffset;
    int pagAtual = scrollOffset.x/scroll.frame.size.width;
    if(pagAtual != 0) { //always release all imageViews and load the current and next
    for(int iCnt = 0; iCnt < [scroll.subviews count]; iCnt++) {
        UIView *viewLiberar = [scroll.subviews objectAtIndex:iCnt];
     if ([viewLiberar isKindOfClass:UIImageView.class]) {
        [viewLiberar removeFromSuperview];
         viewLiberar = nil;
       }
     }
    }
    
    if(pageOnScrollView < ((int)scrollOffset.x/scroll.frame.size.width))
     { 
    
     //load the next page
    [self loadNextPage:(pagAtual)];
    [self loadNextPage:(pagAtual + 1)];
     }
    else if(pageOnScrollView > ((int)scrollOffset.x/scroll.frame.size.width))
      {
      if(pagAtual>0)[self loadNextPage:((int)scrollOffset.x/scroll.frame.size.width)-1];
        }
    
         pageOnScrollView=scrollOffset.x/scroll.frame.size.width;
          }
    
    It works!
     

Share This Page