Question

Im trying to refactor my code for better memory management and performance. I have an array of 12 images, but I want to load them as needed, not all at the same time. So maybe the current image -1, the current image and the current image +1. I viewed some of the answers on SO but found them unclear. I felt it would be clearer if I posted my code for reference.

//scroll view set up
- (void)scrollViewSetUp
{


    self.scrollview.delegate = self;
    for (int i = 0; i < _images.count; i++)
    {
        CGRect frame;
        frame.origin.x = self.scrollview.frame.size.width * i;
        frame.size = self.scrollview.frame.size;
        self.scrollview.pagingEnabled = YES;

        UIImageView *subview = [[UIImageView alloc] initWithFrame:frame];
        subview.image = [UIImage imageNamed:[_images objectAtIndex:i]];
        [self.scrollview addSubview:subview];
}

        self.scrollview.contentSize =  CGSizeMake(self.scrollview.frame.size.width * _images.count, self.scrollview.frame.size.height);
        self.automaticallyAdjustsScrollViewInsets = NO;

        //page control ie .... at bottom of
        self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(100.0,0.0,100.0,40.0)];
       [self.pageControl setNumberOfPages:_images.count];
       [self.pageControl setCurrentPage:0];
       self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
       self.pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
       [self.pageControl setBackgroundColor:[UIColor clearColor]];
       [self.viewForPageControl addSubview:self.pageControl];
       [self.viewForPageControl setBackgroundColor:[UIColor clearColor]];
 }



#pragma mark - UIScrollView Delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    int newOffset = scrollView.contentOffset.x;
    int newPage = (int)(newOffset/(scrollView.frame.size.width));
    [_pageControl setCurrentPage:newPage];
}

Any advice or direction is greatly appreciated.

Était-ce utile?

La solution

As per Gman's request I reposted my comment.

This tutorial will help you:

http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content

Autres conseils

You could try to reuse your UIImageView as they go offscreen... but you'll be just trying to redo what UITableview already does. UITableView with a custom UITableViewCell would take care of notifying you when new cells comes visible and dequeuing the ones that are offscreen.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = @"yourCustomImageCell";
       //look if there are cells that could be reused
    CustomImageCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {   //if not, create a new one
        cell = [[CustomImageCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }

    //Set your image here

    return cell;
}

Not sure if this is the best way of doing it, but it's an option.

You can set the tag property on your UIImageView to correspond to your index in your loop when setting the scrollView.

Then in your scrollViewDidEndDecelerating method:

for (int i = 0; i < _salonImages.count; i++)
    {    

    if(i == (newPage-1))
    {
     UIImageView *imgView = (UIImageView*)[self.scrollView viewWithTag:newPage-1];
     imgView.image = [_images objectAtIndex:newPage-1];
    }
    else if(i == newPage)
    {
     UIImageView *imgView = (UIImageView*)[self.scrollView viewWithTag:newPage];
     imgView.image = [_images objectAtIndex:newPage];
    } 
    else if(i == (newPage + 1 ))
    {
     UIImageView *imgView = (UIImageView*)[self.scrollView viewWithTag:newPage+1];
     imgView.image = [_images objectAtIndex:newPage+1];
    }
    else
    {
     UIImageView *imgView = (UIImageView*)[self.scrollView viewWithTag:i];
     imgView.image = nil;
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top