1.APP进入加载广告视图展示:
-(void)initAdvView{
// 判断图片是否已经存在 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"loading.png"]]; NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL isDir = FALSE; BOOL isExit = [fileManager fileExistsAtPath:filePath isDirectory:&isDir]; if (isExit) { NSLog(@"存在"); _advImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, screen_width, screen_height)]; // [_advImage setImage:[UIImage imageNamed:@"loading.png"]]; [_advImage setImage:[UIImage imageWithContentsOfFile:filePath]]; [self.window addSubview:_advImage]; [self performSelector:@selector(removeAdvImage) withObject:nil afterDelay:3]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //加载启动广告并保存到本地沙盒,因为保存的图片较大,每次运行都要保存,所以注掉了 // [self getLoadingImage]; }); }else{ NSLog(@"不存在"); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 不存在则网络请求,下载图片保存 [self getLoadingImage]; }); } }
// 图片移除 -(void)removeAdvImage{ [UIView animateWithDuration:0.3f animations:^{ _advImage.transform = CGAffineTransformMakeScale(0.5f, 0.5f); _advImage.alpha = 0.f; } completion:^(BOOL finished) { [_advImage removeFromSuperview]; }]; }
网络获取图片:
//获取启动广告图片,采用后台推送时执行请求 -(void)getLoadingImage{ //分辨率 CGFloat scale_screen = [UIScreen mainScreen].scale; NSLog(@"%.0f %.0f",screen_width*scale_screen,screen_height*scale_screen); int scaleW = (int)screen_width*scale_screen; int scaleH = (int)screen_height*scale_screen; NSString *urlStr = [NSString stringWithFormat:@"http://api.meituan.com/config/v1/loading/check.json?app_name=group&app_version=5.7&ci=1&city_id=1&client=iphone&movieBundleVersion=100&msid=48E2B810-805D-4821-9CDD-D5C9E01BC98A2015-07-15-15-51824&platform=iphone&resolution=%d%@%d&userid=10086&utm_campaign=AgroupBgroupD100Fa20141120nanning__m1__leftflow___ab_pindaochangsha__a__leftflow___ab_gxtest__gd__leftflow___ab_gxhceshi__nostrategy__leftflow___ab_i550poi_ktv__d__j___ab_chunceshishuju__a__a___ab_gxh_82__nostrategy__leftflow___ab_i_group_5_3_poidetaildeallist__a__b___b1junglehomepagecatesort__b__leftflow___ab_gxhceshi0202__b__a___ab_pindaoquxincelue0630__b__b1___ab_i550poi_xxyl__b__leftflow___ab_i_group_5_6_searchkuang__a__leftflow___i_group_5_2_deallist_poitype__d__d___ab_pindaoshenyang__a__leftflow___ab_b_food_57_purepoilist_extinfo__a__a___ab_waimaiwending__a__a___ab_waimaizhanshi__b__b1___ab_i550poi_lr__d__leftflow___ab_i_group_5_5_onsite__b__b___ab_xinkeceshi__b__leftflowGhomepage&utm_content=4B8C0B46F5B0527D55EA292904FD7E12E48FB7BEA8DF50BFE7828AF7F20BB08D&utm_medium=iphone&utm_source=AppStore&utm_term=5.7&uuid=4B8C0B46F5B0527D55EA292904FD7E12E48FB7BEA8DF50BFE7828AF7F20BB08D&version_name=5.7",scaleW,@"%2A",scaleH]; [[NetworkSingleton sharedManager] getAdvLoadingImage:nil url:urlStr successBlock:^(id responseBody){ NSLog(@"获取启动广告图片成功"); NSMutableArray *dataArray = [responseBody objectForKey:@"data"]; NSLog(@"dataArray:%@",dataArray); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ if (dataArray.count>0) { NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[dataArray[0] objectForKey:@"imageUrl"]]]; UIImage *image = [UIImage imageWithData:data]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"loading.png"]]; // 保存文件的名称 // BOOL result = [UIImagePNGRepresentation() writeToFile: filePath atomically:YES]; // 保存成功会返回YES NSLog(@"paths:%@ %@",paths,filePath); [UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES]; } }); } failureBlock:^(NSString *error){ NSLog(@"获取启动广告图片失败:%@",error); }]; }
2.刷新自定义gif图片:
-(void)setUpTableView{ //添加下拉的动画图片 //设置下拉刷新回调 [self.tableView addGifHeaderWithRefreshingTarget:self refreshingAction:@selector(refreshData)]; //设置普通状态的动画图片 NSMutableArray *idleImages = [NSMutableArray array]; for (NSUInteger i = 1; i<=60; ++i) { UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_anim__000%zd",i]]; [idleImages addObject:image]; } [self.tableView.gifHeader setImages:idleImages forState:MJRefreshHeaderStateIdle]; //设置即将刷新状态的动画图片 NSMutableArray *refreshingImages = [NSMutableArray array]; for (NSInteger i = 1; i<=3; i++) { UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_loading_0%zd",i]]; [refreshingImages addObject:image]; } [self.tableView.gifHeader setImages:refreshingImages forState:MJRefreshHeaderStatePulling]; //设置正在刷新是的动画图片 [self.tableView.gifHeader setImages:refreshingImages forState:MJRefreshHeaderStateRefreshing]; //马上进入刷新状态 [self.tableView.gifHeader beginRefreshing]; }