简体   繁体   English

调整代码以包括延迟加载-滚动视图和图像

[英]Adjusting code to include Lazy loading - scrollview and images

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. 我有12张图片的阵列,但是我想根据需要加载它们,而不是同时加载所有图片。 So maybe the current image -1 , the current image and the current image +1 . 因此,也许当前图像-1 ,当前图像和当前图像+1 I viewed some of the answers on SO but found them unclear. 我查看了关于SO的一些答案,但发现它们不清楚。 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. 任何建议或指示,将不胜感激。

You could try to reuse your UIImageView as they go offscreen... but you'll be just trying to redo what UITableview already does. 您可以尝试在屏幕外显示时重用UIImageView ...,但是您将尝试重做UITableview已经执行的操作。 UITableView with a custom UITableViewCell would take care of notifying you when new cells comes visible and dequeuing the ones that are offscreen. UITableView一个自定义UITableViewCell将采取通知您,当新的细胞来可见光和出队是屏幕外的那些照顾。

- (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. 设置scrollView时,可以在UIImageView上将tag属性设置为与循环中的索引相对应。

Then in your scrollViewDidEndDecelerating method: 然后在您的scrollViewDidEndDecelerating方法中:

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;
    }
}

As per Gman's request I reposted my comment. 根据Gman的要求,我转贴了我的评论。

This tutorial will help you: 本教程将帮助您:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM