iOS优化内存的横向ScrollView

该博客探讨了如何在iOS应用中优化ScrollView的内存使用,特别是在实现横向滑动展示图片时。通过确保每次滚动结束时只加载屏幕可见的1-3张图片,如从1、2、3切换到2、3、4,实现平滑滚动的同时减少内存负担。这种方法确保了用户在浏览过程中体验流畅,而不会因大量图片加载导致内存问题。
摘要由CSDN通过智能技术生成

原理就是ScrollView中同时只有1、2、3张图片,每次翻动结束后,重新设置这三张图片,如1、2、3,原来是2,向右翻动结束减速后将1、2、3换成2、3、4,由于是结束滚动后替换的,所以看上去跟平滑滚动是一样的


#import "ViewController.h"

@implementation ViewController
{
    UIScrollView *_mainScrollView;
    NSMutableArray *_imagePathArray;
    NSInteger _currentIndex;
    CGFloat _width;
    CGFloat _height;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _width = self.view.frame.size.width;
    _height = self.view.frame.size.height;
    self.view.backgroundColor = [UIColor whiteColor];
    _mainScrollView = [[UIScrollView alloc] init];
    _mainScrollView.frame = CGRectMake(0, 0, _width , _height);
    _mainScrollView.delegate = self;
    _mainScrollView.pagingEnabled = YES;
    _mainScrollView.bounces = NO;
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    _mainScrollView.contentSize = CGSizeMake(_width*3, _height);
    [self.view addSubview:_mainScrollView];
    
    _imagePathArray = [[NSMutableArray alloc] init];
    for(int i=1;i<7;i++)
    {
        NSString *path = [NSString stringWithFormat:@"%@/%d.jpg",[[NSBundle mainBundle] resourcePath],i];
        [_imagePathArray addObject:path];
    }
    
    _currentIndex = 0;
    
    [self loadImageView];
}

- (void)loadImageView
{
    // 移除之前加载的
    for(UIView *view in _mainScrollView.subviews)
    {
        [view removeFromSuperview];
    }
    
    // 当前页
    
    UIImage *currentImage = [[UIImage alloc] initWithContentsOfFile:[_imagePathArray objectAtIndex:_currentIndex]];
    UIImageView *currentImageView = [[UIImageView alloc] initWithImage:currentImage];
    currentImageView.frame = CGRectMake(_width, 0, _width, _height);
    [_mainScrollView addSubview:currentImageView];
    
    // 左侧页
    UIImage *preImage = [[UIImage alloc] initWithContentsOfFile:_currentIndex-1<0?[_imagePathArray lastObject]:[_imagePathArray objectAtIndex:_currentIndex-1]];
    UIImageView *preImageView = [[UIImageView alloc] initWithImage:preImage];
    preImageView.frame = CGRectMake(0, 0, _width, _height);
    [_mainScrollView addSubview:preImageView];
    
    // 右侧页
    UIImage *nextImage = [[UIImage alloc] initWithContentsOfFile:_currentIndex+1==_imagePathArray.count?[_imagePathArray firstObject]:[_imagePathArray objectAtIndex:_currentIndex+1]];
    UIImageView *nextImageView = [[UIImageView alloc] initWithImage:nextImage];
    nextImageView.frame = CGRectMake(_width*2, 0, _width, _height);
    [_mainScrollView addSubview:nextImageView];
    
    
    [_mainScrollView setContentOffset:CGPointMake(_width, 0)];
}

//翻页
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"scrollView.contentOffset.x = %.0f",scrollView.contentOffset.x);
    int index = scrollView.contentOffset.x/_width;
    if(index == 0)
    {
        _currentIndex = _currentIndex-1<0?_imagePathArray.count-1:_currentIndex-1;
        [self loadImageView];
    }
    if(index == 2)
    {
        _currentIndex = _currentIndex+1==_imagePathArray.count?0:_currentIndex+1;
        [self loadImageView];
    }
}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值