您的位置:首页 > 理论基础 > 计算机网络

多个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


最后附上一张效果图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息