动态改变启动图标

动态改变启动图标 -- 一开始想到的就是 启动图标能不能从网站获取图片在加载到app上 后来没找到方法

仔细阅读了许多博客之后发现 其实你看到的不是一张图片,而是两张

类似百度云

这是第一张

这是第二张

至于为什么要用两张?

LaunchScreen.xib或者LaunchScreen.Storyboard,只要你的app启动过,那张图片就永远的缓存在app里了,以后每次启动都会出现。  所以这地张图片是固定的

那么 显示两张要怎么做?

第一步:

获取LaunchScreen.storyboard里的UIViewController,UIViewController 的identifer是LaunchScreen

UIViewController *viewController = [[UIStoryboard storyboardWithName:@"LaunchScreen" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"LaunchScreen"];
UIView *launchView = viewController.view;
UIImageView  * Imageview= [[UIImageView  alloc]initWithFrame:[UIScreen mainScreen].bounds];
[launchView addSubview:Imageview];
[self.view addSubview:launchView];

 

这一步是获取上次网络请求下来的图片,如果存在就展示该图片,如果不存在就展示本地保存的名为test的图片

NSMutableData * data = [[NSUserDefaults standardUserDefaults]objectForKey:@"imageu"];
if (data.length>0) {
   Imageview.image = [UIImage imageWithData:data];
}else{

Imageview.image = [UIImage imageNamed:@"Test"];
}

 下面这段代码,是调用AFN下载文件的方法,异步方式下载,但是在这里异步方式下载有一个问题,就是这次下载完成的图片,下次启动时才会展示,读者可以换成同步的,但是同步下载会有时间延迟,用户体验不好,下载完图片后,将图片以二进制的形式存在本地,笔者用的是userdefault,这是不科学的,读者可以存在其他文件夹

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL *URL = [NSURL URLWithString:@"http://s16.sinaimg.cn/large/005vePOgzy70Rd3a9pJdf&690"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {

  NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
  return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
  NSLog(@"File downloaded to: %@", filePath);

  NSData * image = [NSData dataWithContentsOfURL:filePath];
  [[NSUserDefaults standardUserDefaults]setObject:image forKey:@"imageu"];
}];
[downloadTask resume];

 这段代码,可以实现第二张图片有3D的动画效果,动画结束后,进行同步的网络请求,请求的是广告页图片,之所以用同步的是因为,如果用同步的话主页面显示后,图片才会加载完成显示

[UIView animateWithDuration:6.0f delay:0.0f options:UIViewAnimationOptionBeginFromCurrentState animations:^{

        //launchView.alpha = 0.0f;
        launchView.layer.transform = CATransform3DScale(CATransform3DIdentity, 1.5f, 1.5f, 1.0f);
    } completion:^(BOOL finished) {


                NSString * ad_imgUrl  = @"http://www.uimaker.com/uploads/allimg/121217/1_121217093327_1.jpg";
                AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
                [BBLaunchAdMonitor showAdAtPath:ad_imgUrl
                                         onView:appDelegate.window.rootViewController.view
                                   timeInterval:5
                               detailParameters:@{@"carId":@(12345), @"name":@"奥迪-品质生活"}];
                   [launchView removeFromSuperview];
    }];

 BBLaunchAdMonitor 可自行百度下载

 

 

 

 


 

posted @ 2017-02-11 11:23  唐宋元明清。  阅读(606)  评论(0编辑  收藏  举报