多个SDCycleScrollView轮播图加载网络图片减少内存使用.
2016-01-18 13:15
537 查看
**多个SDCycleScrollView轮播图加载网络图片减少内存使用.** 由于自己的工程需要用到多个轮播图,结果加了没几个轮播图Xcode显示Memory内存占用就直逼两百兆,瞬间我就醉了,这要多加几个轮播图还不得瞬间Cash. 经过多方查证资料研究了解原因是:SDCycleScrollView中对网络加载轮播图的时候是吧网络图片直接缓存在缓存中,自然使用一个轮播图就Memory增加二三十兆, 如果是加载本地图片的话就不会占用缓存,继而降低了缓存使用. 好了了解了原因出在哪里,就要想解决办法思路有两个: 1,提前把图片下载好保存在一个数组中,作为本地图片给轮播图使用, 2,提前把图片下载并保存到沙盒或者数据库中进行存储,然后调出来给轮播图使用,下次启动轮播图的时候先在本地查找,如果没有再进行网络请求,继而保存到沙盒或者数据库中再调出来作文本地图片使用. 上面两个方法都需要给轮播图一个占位图片前期进行展示.避免出现轮播图位置空白的情况, 这里选择了第一种方法,较为简单. 准备工作: 需要导入SDWebImage 和 SDCycleScrollView 这两个第三方包,这个相信大家应该会有吧, 好了,不再啰嗦直接上代码.
#import "RootTwoViewController.h" #import "UIImageView+WebCache.h" #import "SDCycleScrollView.h" #define kScreenWidth [UIScreen mainScreen].bounds.size.width #define kScreenHeight [UIScreen mainScreen].bounds.size.height #define kHotCycleLabelFont 15 @interface RootTwoViewController ()<SDCycleScrollViewDelegate, UITableViewDataSource, UITableViewDelegate> // 定义属性接口 // 轮播图 @property (nonatomic, strong)SDCycleScrollView * cycleSC; // 轮播图图片数组 @property (nonatomic, strong)NSMutableArray * imagesArray; @property (nonatomic, strong)NSMutableArray * imagesArrayThread; // 轮播图标题 @property (nonatomic, strong)NSMutableArray * titlesArray; @property (nonatomic, strong)NSThread * myImageArrayThread; @property (nonatomic, strong)UITableView * myTableView; @end @implementation RootTwoViewController #pragma mark -- 移除通知 - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:@"IMAGEARRAY" object:nil]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor whiteColor]; self.navigationController.navigationBar.translucent = NO; [self addNsNotification]; [self addSubViews]; // [self addCycleScrollView]; [self addSubTableViews]; } - (void)addSubTableViews { self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 20) style:(UITableViewStyleGrouped)]; self.myTableView.delegate = self; self.myTableView.dataSource = self; UIView * headerView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, kScreenHeight, 150))]; [headerView addSubview:self.cycleSC]; self.myTableView.tableHeaderView = headerView; [self.view addSubview:self.myTableView]; } // 注册通知 - (void)addNsNotification { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationInfo:) name:@"IMAGEARRAY" object:nil]; } - (void)addCycleScrollView { // 测试数据标题和图片 NSArray * arraytitle = @[@"第一张", @"第二张", @"第三张"]; self.titlesArray = [[NSMutableArray alloc] initWithArray:arraytitle]; self.cycleSC = [SDCycleScrollView cycleScrollViewWithFrame:(CGRectMake(0, 0, kScreenWidth,150)) imagesGroup:self.imagesArrayThread]; NSLog(@"++++++++++++++++++++++++++++Thread%d+++++",[self.imagesArrayThread count]); /** 留作占位代码 当加载图片过多时 或者 添加占位图片时需要添加 * 1,此处 注释代码 * 2,上方ViewDidLoad中注释掉的代码 */ // if ([self.imagesArray count] == 0) { // // UIImage * image = [UIImage imageNamed:@"Example.png"]; // NSArray * arratUrl = @[image, image, image]; // self.imagesArray = [[NSMutableArray alloc] initWithArray:arratUrl]; // self.cycleSC.localizationImagesGroup = self.imagesArray; // // } else { self.cycleSC.localizationImagesGroup = self.imagesArrayThread; // } self.cycleSC.backgroundColor = [UIColor whiteColor]; // 标题背景颜色 self.cycleSC.titleLabelBackgroundColor = [UIColor whiteColor]; // 标题背景高度 self.cycleSC.titleLabelHeight = 200 * 0.1; // 字体属性 self.cycleSC.titleLabelTextColor = [UIColor blackColor]; self.cycleSC.titleLabelTextFont = [UIFont boldSystemFontOfSize:kHotCycleLabelFont]; // self.cycleSC.imageURLStringsGroup = self.imagesArray; self.cycleSC.titlesGroup = self.titlesArray; self.cycleSC.delegate = self; // 图片间隔 self.cycleSC.autoScrollTimeInterval = 2; // 小圆点的属性 self.cycleSC.dotColor = [UIColor redColor]; self.cycleSC.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter; self.cycleSC.pageControlDotSize = CGSizeMake(5, 5); } - (void)addSubViews { // 开辟子线程接收图片 self.myImageArrayThread = [[NSThread alloc] initWithTarget:self selector:@selector(myImageArrayThreadAction) object:nil]; [self.myImageArrayThread start]; } // 子线程实现方法 - (void)myImageArrayThreadAction { self.imagesArrayThread = [[NSMutableArray alloc] init]; for (int i = 0; i < 3; i++) { UIImageView * imageView = [[UIImageView alloc] init]; [imageView sd_setImageWithURL:[NSURL URLWithString:@"http://ac-YML68WwP.clouddn.com/图片网址.jpg"]]; while (imageView.image == NULL) { sleep(0.1); } NSLog(@"%@-------", imageView.image); [self.imagesArrayThread addObject:imageView.image]; } [[NSNotificationCenter defaultCenter] postNotificationName:@"IMAGEARRAY" object:nil userInfo:@{@"UI":@"Over"}]; } // 通知实现方法 - (void)notificationInfo:(NSNotification *)notification { NSLog(@"终于接收到通知了----------------%@", self.imagesArrayThread[0]); dispatch_async(dispatch_get_main_queue(), ^{ [self addCycleScrollView]; [self addSubTableViews]; // [self.myTableView reloadData]; // 可刷新可不刷新 看情况而定 }); [self.myImageArrayThread cancel]; } #pragma mark -- myTableView代理 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 5; } - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { return 3; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString * identifier = @"myTableViewCell"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier]; cell.textLabel.text = @"终于快要出来了"; } return cell; } #pragma mark -- 轮播图唯一代理方法 - (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index { NSLog(@"%d", index); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
最后附上一张效果图
相关文章推荐
- 按右键另存图片只能存BMP
- photoshop去除图片上的水印
- IE7降低内存和降低CPU的几个技巧
- upload上传单张图片
- 图片引发的溢出危机(图)
- 如何高效的使用内存
- DOS下内存的配置
- XP/win2003下发现1G的内存比512M还慢的解决方法
- C#实现把彩色图片灰度化代码分享
- PowerShell实现动态获取当前脚本运行时消耗的内存
- C#将图片和字节流互相转换并显示到页面上
- C#监控文件夹并自动给图片文件打水印的方法
- 纯CSS实现的当鼠标移上图片添加阴影效果代码
- C#实现把dgv里的数据完整的复制到一张内存表的方法
- C#实现打开画图的同时载入图片、最大化显示画图窗体的方法
- SQL语句实现查询SQL Server内存使用状况
- 随鼠标移动的图片或文字特效代码
- CSS 图片横向排列实现代码
- C#实现将Email地址转成图片显示的方法
- 超级经典一套鼠标控制左右滚动图片带自动翻滚