iOS网络加载启动图

本文介绍了如何在iOS应用中实现网络加载启动图。首先从LaunchScreen.storyboard中获取UIViewController,添加UIImageView并设置图片。根据默认存储的数据加载图片,若无则显示默认图片。接着使用AFNetworking下载图片,并保存到文档目录。最后,通过UIView动画实现3D过渡效果,并在动画完成后发起网络请求加载广告页面图片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 这一步是获取 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 下载文件的方法,异步
   
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 :3 .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://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=%E5%9B%BE%E7%89%87&pn=0&spn=0&di=125546894880&pi=&rn=1&tn=baiduimagedetail&ie=utf-8&oe=utf-8&cl=2&lm=-1&cs=1003704465%2C1400426357&os=4246966059%2C4277404619&simid=4210997991%2C798394471&adpicid=0&ln=30&fr=ala&fm=&sme=&cg=&bdtype=0&oriquery=&objurl=http%3A%2F%2Fpic25.nipic.com%2F20121112%2F5955207_224247025000_2.jpg&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3Bgtrtv_z%26e3Bv54AzdH3Ffi5oAzdH3FnAzdH3F0nAzdH3F0al8l0mhdj9v1c9n_z%26e3Bip4s&gsm=0" ;
               
AppDelegate *appDelegate = ( AppDelegate *)[[ UIApplication sharedApplication ] delegate ];
                [
BBLaunchAdMonitor showAdAtPath :ad_imgUrl
                                        
onView :appDelegate. window . rootViewController . view
                                  
timeInterval : 5
                              
detailParameters : @{ @"carId" : @(0001) , @"name" : @" 0007 " } ];
                   [launchView
removeFromSuperview ];
               
       

    }];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值