通过AFNetworking下载网络闪屏用的图片并存储到本地同时获取header头相关信息

使用场景:

       有不少App启动时会加载启动页和闪屏广告,对闪屏广告时间控制一般有好几种方案

这里讲一下我在实际开发中最常用的一种解决方案,大体思路是应用启动时先进入闪屏加载流程

在获取广告图时将expire图片失效时间加到header头中来控制广告的过期时间。将首次加载广告

的ctime和expire及广告的key存储到本地sqlite3数据库中,这样只要在有效期expire内就显示本地之

前加载的广告就可以了。

首次加载广告主要代码如下:

/**
 *  系统启动时调用
 *
 *  @param application
 *  @param launchOptions
 */
- (void)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    _lock = [[NSRecursiveLock alloc] init];
    
    //启动广告图
    _splashImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, FD_SCREEN_MAIN_WIDTH, FD_SCREEN_MAIN_HEIGHT-114)];
    _splashImageView.alpha = 0;
    _splashImageView.contentMode = UIViewContentModeScaleAspectFill;
    
    UIView *_botIconView = [[UIView alloc] initWithFrame:CGRectMake(0, FD_SCREEN_MAIN_HEIGHT-114, FD_SCREEN_MAIN_WIDTH, 114)];
    _botIconView.backgroundColor = [UIColor whiteColor];
    
    //底部logo
    UIImageView *_iconImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, (114-77)/2+20, FD_SCREEN_MAIN_WIDTH, 77)];
    _iconImgView.image = [UIImage imageNamed:@"bg_splash_screen"];
    _iconImgView.backgroundColor = [UIColor whiteColor];
    [_botIconView addSubview:_iconImgView];
    [_splashImageView addSubview:_botIconView];
    
    _advDic = [self isLocalExpires];
    
    //当前时间
    NSTimeInterval _currentTime = [[NSDate date] timeIntervalSince1970];
    if (([_advDic count] && ((_currentTime-[[_advDic objectForKey:@"ctime"] intValue]) <= ADV_LIFE_EXPIRE) && ((_currentTime-[[_advDic objectForKey:@"ctime"] intValue]) < [[_advDic objectForKey:@"expire"] intValue]))) {//已经缓存过
        
        //将启动广告加入主视图window中
        [self addWindow:_splashImageView];
        UIImage *_image = [UIImage readWithFile:[NSString stringWithFormat:@"%@%@", SPLASH_SAVE_VALUE, @".png"]];
        
        [_lock lock];
        //动画滑出
        [UIView animateWithDuration:.3f animations:^{
            _splashImageView.alpha = 1;
            _splashImageView.image = _image;
        }];
        
        [timer invalidate];
        timer = nil;
        timer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(enterMainLogic) userInfo:nil repeats:NO];
        [_lock unlock];
        
    }else{ //获取远程图片
        
        //判断是否wifi
        if ([SystemUtility IsEnableWIFI]) {
            [self downImageWithSplash];
        }else{
            [self enterMainLogic];
        }
    }
}

/**
 * 将启动广告加入主视图window中
 */
- (void)addWindow:(UIImageView *)aImageView
{
    UIWindow *window = ((AppDelegate*)[UIApplication sharedApplication].delegate).window;
    window.backgroundColor = [UIColor whiteColor];
    
    UIViewController *rvc = [[UIViewController alloc] init];
    rvc.view.frame = CGRectMake(0, 0, FD_SCREEN_MAIN_WIDTH, FD_SCREEN_MAIN_HEIGHT-114);
    [rvc.view addSubview:aImageView];
    
    window.rootViewController = rvc;
    [window makeKeyAndVisible];
}

/**
 *  读取本地图片
 *
 *  @return
 */
- (NSDictionary *)isLocalExpires {
    
    return [[DbAdv singleton] readAdvByKey:SPLASH_SAVE_VALUE];
}

/**
 * 从服务器加载下载图片
 */
-(void) downImageWithSplash {
    
    NSString *_imgUrl = [NSString stringWithFormat:@"%@/get", [[NSObject DTGetConfig] objectForKey:@"apiUrl"]];
    NSURL *url = [NSURL URLWithString:_imgUrl];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
    
    AFHTTPRequestOperation *afOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    afOperation.inputStream   = [NSInputStream inputStreamWithURL:url];
    
    NSArray *pathAry = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    
    //并给文件起个文件名
    NSString *fileName          = [NSString stringWithFormat:@"%@%@", SPLASH_SAVE_VALUE, @".png"];
    NSString *filePath          = [[pathAry objectAtIndex:0] stringByAppendingPathComponent:fileName];
    afOperation.outputStream    = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];
    
    //已完成下载
    [afOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
                
        //保存进sqlite(测试用)
        NSString *expire = [operation.response.allHeaderFields objectForKey:@"expire"];
        
        if(([expire intValue]>0)){
            //显示闪屏广告
            _splashImageView.image = [UIImage imageWithContentsOfFile:fileName];
            [_splashImageView setNeedsDisplay];
            
            //更新到数据库
            [[DbAdv singleton] updateToDB:SPLASH_SAVE_VALUE expire:expire];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    [afOperation start];
    
    //进入app主流程
    [self enterMainLogic];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值