IOS使用scrollview显示大量图片,实现缓存,节省内存

一般大家在用scrollview加载图片的时候少量还好,若是大量图片则很容易引起内存不足,这里介绍一种省内存的办法,只加载前后加当前一共三张即可,然后来回切换加载图片,相当的省内存,用到两个自己写的类,一个是Myscrollview,一个是easyImageLoader,不罗嗦,上代码

#import <UIKit/UIKit.h>
@protocol MyScrollViewDelegate <NSObject>;
-(void)singleTapGesture;
@end

@interface MyScrollView : UIScrollView <UIScrollViewDelegate>
{
UIImage *image;
UIImageView *imageView;
}

@property (assign, nonatomic) id<MyScrollViewDelegate>  scollViewDelegate;
@property (nonatomic, retain) UIImage *image;
@property (nonatomic, retain) UIImageView    *imageView;

@end

#import "MyScrollView.h"


@implementation MyScrollView


@synthesize image;
@synthesize imageView;
@synthesize scollViewDelegate;


#pragma mark -
#pragma mark === Intilization ===
#pragma mark -
- (id)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame]))
{
self.delegate = self;
self.minimumZoomScale = 0.5;
self.maximumZoomScale = 2.5;
self.showsVerticalScrollIndicator = NO;
self.showsHorizontalScrollIndicator = NO;
imageView  = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
imageView.contentMode = UIViewContentModeScaleAspectFit;

[self addSubview:imageView];
    }
    return self;
}

- (void)setImage:(UIImage *)img
{
imageView.image = img;
}

#pragma mark -
#pragma mark === UIScrollView Delegate ===
#pragma mark -
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imageView;
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale                                                                        //双指放大缩小
{
//NSLog(@"%s", _cmd);
CGFloat zs = scrollView.zoomScale;
zs = MAX(zs, 1.0);
zs = MIN(zs, 2.0);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
scrollView.zoomScale = zs;
[UIView commitAnimations];
}

#pragma mark -
#pragma mark === UITouch Delegate ===
#pragma mark -
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2)                                                      //双击放大图片
{
CGFloat zs = self.zoomScale;
zs = (zs == 1.0) ? 2.0 : 1.0;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
self.zoomScale = zs;
[UIView commitAnimations];
}
    if ([scollViewDelegate respondsToSelector:@selector(singleTapGesture)]) {
        [scollViewDelegate performSelector:@selector(singleTapGesture)];
    }

}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {

}

-(BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view{

    return NO;
}
@end






#import <UIKit/UIKit.h>
#import "MyScrollView.h"

@interface easyImageLoader : UIScrollView<MyScrollViewDelegate,UIScrollViewDelegate>
@property (strong, nonatomic) NSMutableArray  *viewControllers;
@property (strong, nonatomic) NSMutableArray *_imageArray;
@property (strong, nonatomic) NSMutableArray *_imagePathArray;
@property (strong, nonatomic) NSMutableArray *scrollViewArray;

///供固定的大量图片使用
-(void)loadImageWithImageArray:(NSMutableArray*)imageArray;

供网络获取而来的图片使用,可刷新
-(void)loadImageWithImageArrayFromPathArray:(NSMutableArray *)imagePathArray;
-(void)reloadImageAt:(NSInteger)index;

@end




#import "easyImageLoader.h"

@implementation easyImageLoader
@synthesize viewControllers;
@synthesize _imageArray;
@synthesize _imagePathArray;
@synthesize scrollViewArray;



- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.delegate = self;
        // Initialization code
    }
    return self;
}

-(void)loadImageWithImageArray:(NSMutableArray*)imageArray{
    _imageArray = [[NSMutableArray alloc]initWithArray:imageArray];
    [self loadImageShow];
}



-(void)loadImageWithImageArrayFromPathArray:(NSMutableArray *)imagePathArray{
    _imagePathArray = [[NSMutableArray alloc]initWithArray:imagePathArray];
    for (NSString *path in imagePathArray) {
            [_imageArray addObject:NULL];
    }
}

-(void)reloadImageAt:(NSInteger)index{

    UIViewController  *controller2 = [self.viewControllers objectAtIndex:index];
    if ((NSNull *)controller2 != [NSNull null])
    {
        for (UIImageView *imageView in controller2.view.subviews) {
            imageView.image = nil;
        }
    }
    [self.viewControllers replaceObjectAtIndex:index withObject:[NSNull null]];

    UIImage *image = nil;
    if (_imagePathArray.count) {
        image = [[UIImage alloc] initWithContentsOfFile: [_imagePathArray objectAtIndex:index]];
    }
    else{
        image = [[UIImage alloc]init];
        image = [_imageArray objectAtIndex:index];
    }
    [_imageArray replaceObjectAtIndex:index withObject:image];
    [self loadScrollViewWithPage:index];
    [image release];
}


-(void)singleTapGesture{

}

-(void)loadImageShow{    
    NSLog(@"page89023147987491028");
    if ([self.viewControllers count]) {

    }
    else{
        NSMutableArray *controllers = [[NSMutableArray alloc] init];
        for (unsigned i = 0; i < [_imageArray count]; i++)
        {
            [controllers addObject:[NSNull null]];
        }
        self.viewControllers = controllers;
        [controllers release];

    }
    self.contentSize = CGSizeMake(self.frame.size.width * [_imageArray count], self.frame.size.height);

    CGFloat pageWidth = self.frame.size.width;
    int page = floor((self.contentOffset.x - pageWidth / 2) / pageWidth) + 1;

    if (page<[_imageArray count]-1) {
        if (page>0) {
            UIViewController  *controller1 = [self.viewControllers objectAtIndex:page-1];
            if ((NSNull *)controller1 != [NSNull null])
            {
                for (UIImageView *imageView in controller1.view.subviews) {
                    imageView.image = nil;
                }
            }
        }
        UIViewController  *controller2 = [self.viewControllers objectAtIndex:page];
        if ((NSNull *)controller2 != [NSNull null])
        {
            for (UIImageView *imageView in controller2.view.subviews) {
                imageView.image = nil;
            }
        }
        UIViewController  *controller3 = [self.viewControllers objectAtIndex:page+1];
        if ((NSNull *)controller3 != [NSNull null])
        {
            for (UIImageView *imageView in controller3.view.subviews) {
                imageView.image = nil;
            }
        }
        NSLog(@"lologPageHeieI   1");

        if (page>0) {
            [self.viewControllers replaceObjectAtIndex:page-1 withObject:[NSNull null]];
        }
        NSLog(@"lologPageHeieI     2");
        [self.viewControllers replaceObjectAtIndex:page withObject:[NSNull null]];
        [self.viewControllers replaceObjectAtIndex:page+1 withObject:[NSNull null]];

        if (page >0) {
            [self loadScrollViewWithPage:page-1];
        }
        [self loadScrollViewWithPage:page];
        [self loadScrollViewWithPage:page+1];
    }

    NSLog(@"lologPageHeieI     3");
}


-(UIViewController*)getTheController:(NSInteger)page viewController:(UIViewController*)viewController{

    NSLog(@"wwwwwww");

    UIImage *getImage = [_imageArray objectAtIndex:page];

    MyScrollView  *scrollViewForScale = [[MyScrollView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    scrollViewForScale.scollViewDelegate = self;

    scrollViewForScale.image = getImage;
    [scrollViewArray addObject:scrollViewForScale];


    [viewController.view addSubview:scrollViewForScale];

    [scrollViewForScale release];
    scrollViewForScale = nil;


    NSLog(@"ttttttttttt");

    return viewController;

}


- (void)loadScrollViewWithPage:(int)page
{

    NSLog(@"pagezzy");
    if (page < 0)
        return;
    if (page >= [_imageArray count])
        return;

    // replace the placeholder if necessary
    UIViewController  *controller = [self.viewControllers objectAtIndex:page];
    if ((NSNull *)controller == [NSNull null])
    {
        controller = [[UIViewController alloc] init];
        [self getTheController:page viewController:controller];
        [self.viewControllers replaceObjectAtIndex:page withObject:controller];
        [controller release];
    }

    // add the controller's view to the scroll view
    if (controller.view.superview == nil)
    {
        CGRect frame = self.frame;
        frame.origin.x = frame.size.width * page;
        frame.origin.y = 0;
        controller.view.frame = frame;
        [self addSubview:controller.view];
    }
    NSLog(@"pagezzyyyyyy");
}




- (void)scrollViewDidScroll:(UIScrollView *)sender
{

    NSLog(@"page1233");
    // We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
    // which a scroll event generated from the user hitting the page control triggers updates from
    // the delegate method. We use a boolean to disable the delegate logic when the page control is used.

    // Switch the indicator when more than 50% of the previous/next page is visible
    CGFloat pageWidth = self.frame.size.width;
    int page = floor((self.contentOffset.x - pageWidth / 2) / pageWidth) + 1;


    // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
    [self loadScrollViewWithPage:page - 1];
    [self loadScrollViewWithPage:page];
    [self loadScrollViewWithPage:page + 1];


    if (page>2 &&page<[_imageArray count]-3) {
        UIViewController  *controller1 = [self.viewControllers objectAtIndex:page-2];
        if ((NSNull *)controller1 != [NSNull null])
        {
            for (UIImageView *imageView in controller1.view.subviews) {
                imageView.image = nil;
            }
        }

        UIViewController  *controller2 = [self.viewControllers objectAtIndex:page-3];
        if ((NSNull *)controller2 != [NSNull null])
        {
            for (UIImageView *imageView in controller2.view.subviews) {
                imageView.image = nil;
            }
        }

        UIViewController  *controller3 = [self.viewControllers objectAtIndex:page+2];
        if ((NSNull *)controller3 != [NSNull null])
        {
            for (UIImageView *imageView in controller3.view.subviews) {
                imageView.image = nil;
            }
        }

        UIViewController  *controller4 = [self.viewControllers objectAtIndex:page+3];
        if ((NSNull *)controller4 != [NSNull null])
        {
            for (UIImageView *imageView in controller4.view.subviews) {
                imageView.image = nil;
            }
        }
        [self.viewControllers replaceObjectAtIndex:page-2 withObject:[NSNull null]];
        [self.viewControllers replaceObjectAtIndex:page-3 withObject:[NSNull null]];
        [self.viewControllers replaceObjectAtIndex:page+2 withObject:[NSNull null]];
        [self.viewControllers replaceObjectAtIndex:page+3 withObject:[NSNull null]];
    }
    NSLog(@"%d",page);

    // A possible optimization would be to unload the views+controllers which are no longer visible
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值