ios 一行代碼實現(xiàn)iOS項目啟動頁, 包括加載網(wǎng)絡視頻, 本地視頻, 網(wǎng)絡圖片, 本地圖片

一行代碼實現(xiàn)iOS項目啟動頁, 包括加載網(wǎng)絡視頻, 本地視頻, 網(wǎng)絡圖片, 本地圖片

github下載地址:https://github.com/ZHHalsey/AppStart(感覺有用可以點個star)

可以設置倒計時

使用方法很簡單

導入寫好的類ZHMoviePlayerController, 創(chuàng)建一個對象, 然后根據(jù)項目需求調(diào)用下面的兩個對象方法(分加載視頻跟加載圖片,可以是網(wǎng)絡的也可以是本地的)

image

如果加載的時候, 如果只加載網(wǎng)絡圖片不加載本地圖片的話,本地圖片參數(shù)傳入為nil就可以,加載視頻同理

這里展示的demo沒加緩存, 我自己做的項目中加了緩存了,提供下思路在這里:

加緩存的話, 寫入沙盒, 設置一個userdefault, 啟動的時候先去沙盒找, 如果沙盒有的話就加載沙盒的, 沙盒沒有的話就加載網(wǎng)絡的, 然后把需要加載的啟動頁寫入沙盒就行

demo展示放不了視頻, 所以就放幾張圖片, 供大家參考

下面這個是視頻啟動頁,可以點擊按鈕進入應用, 也可以設置幾秒倒計時自動進入

image

下面這個是加載圖片啟動頁, 這里是用的倒計時的加載

image

貼上源碼:

#import "ViewController.h"

@interface ZHMoviePlayerController : ViewController

/**
 *  @param movieURL 網(wǎng)上url視頻
 *  @param localMovieName 本地視頻
 */
- (void)setMoviePlayerInIndexWithURL:(NSURL *)movieURL localMovieName:(NSString *)localMovieName;

/**
 *  @param imageURL 網(wǎng)上url圖片
 *  @param localImageName 本地圖片
 *  @param timeCount 倒計時時間
 */

- (void)setImageInIndexWithURL:(NSURL *)imageURL localImageName:(NSString *)localImageName timeCount:(int)timeCount;

@end
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width


#import "ZHMoviePlayerController.h"
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
#import "ViewController.h"

@interface ZHMoviePlayerController ()

@property (nonatomic, strong)AVPlayerViewController *AVPlayer;
@property (nonatomic, strong)UIButton *enterMainButton;
@property (nonatomic, assign) int timeCount;
@property (nonatomic, weak)NSTimer *timer;
@property (nonatomic, weak)NSTimer *timer1;


@end

@implementation ZHMoviePlayerController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
}

- (void)setMoviePlayerInIndexWithURL:(NSURL *)movieURL localMovieName:(NSString *)localMovieName
{
    self.AVPlayer = [[AVPlayerViewController alloc]init];
    // 取消多分屏功能
    self.AVPlayer.allowsPictureInPicturePlayback = NO;
    self.AVPlayer.showsPlaybackControls = false;
    AVPlayerItem *item;
    if (movieURL) {
        NSLog(@"傳入了網(wǎng)絡視頻url過來");
        item = [[AVPlayerItem alloc]initWithURL:movieURL];
    }else if (localMovieName) {
        NSLog(@"加載的是本地的視頻");
        NSString *path =  [[NSBundle mainBundle] pathForResource:@"movie.mp4" ofType:nil];
        NSLog(@"path---%@", path);
        item = [[AVPlayerItem alloc]initWithURL:[NSURL fileURLWithPath:path]];
    }
    AVPlayer *player = [AVPlayer playerWithPlayerItem:item];
    // layer
    AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:player];
    [layer setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    // 填充模式
//    layer.videoGravity = AVLayerVideoGravityResizeAspect; // 保持視頻的縱橫比
    layer.videoGravity = AVLayerVideoGravityResize; // 填充整個屏幕
    self.AVPlayer.player = player;
    [self.view.layer addSublayer:layer];
    [self.AVPlayer.player play];
    
    // 重復播放葛峻。
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playDidEnd:)
                                                 name:AVPlayerItemDidPlayToEndTimeNotification
                                               object:item];
//    [self createLoginBtn]; // 3秒后自動就停止(這里自行選擇)
    [self createLoginBtn1]; // 不點的話 就一直播放視頻
    
}

- (void)setImageInIndexWithURL:(NSURL *)imageURL localImageName:(NSString *)localImageName timeCount:(int)timeCount{
    
    _timeCount = timeCount;
    // http://fimg.yucuizhubao.com/img/start.png
    UIImageView *imagev1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    if (imageURL) {
        NSLog(@"加載的是網(wǎng)絡上的圖片");
        NSData *data = [NSData dataWithContentsOfURL:imageURL];
        UIImage *image1 = [UIImage imageWithData:data];
        imagev1.image = image1;
        
    }
    if (localImageName) {
        NSLog(@"加載的是本地的圖片");
        UIImage *image = [UIImage imageNamed:@"bj.png"];
        imagev1.image = image;
    }
    
    [self.view addSubview:imagev1];
    [self createLoginBtn];
}

// 播放完成代理
- (void)playDidEnd:(NSNotification *)Notification{
    // 重新播放
    [self.AVPlayer.player seekToTime:CMTimeMake(0, 1)];
    [self.AVPlayer.player play];
}

// 用戶不用點擊, 幾秒后自動進入程序
- (void)createLoginBtn
{
    // 進入按鈕
    _enterMainButton = [[UIButton alloc] init];
    _enterMainButton.frame = CGRectMake(SCREEN_WIDTH - 90, 50, 60, 30);
    _enterMainButton.backgroundColor = [UIColor grayColor];
    _enterMainButton.titleLabel.font = [UIFont systemFontOfSize:12];
    _enterMainButton.layer.cornerRadius = 15;
    NSString *title = [NSString stringWithFormat:@"跳過 %d", _timeCount];
    [_enterMainButton setTitle:title forState:UIControlStateNormal];
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(DaoJiShi) userInfo:nil repeats:YES];
    [self.view addSubview:_enterMainButton];
    [_enterMainButton addTarget:self action:@selector(enterMainAction) forControlEvents:UIControlEventTouchUpInside];
}
// 倒計時
- (void)DaoJiShi{
    if (_timeCount > 0) {
        _timeCount -= 1;
        NSString *title = [NSString stringWithFormat:@"跳過 %d", _timeCount];
        [_enterMainButton setTitle:title forState:UIControlStateNormal];
    }else{
        [_timer invalidate];
        _timer = nil;
        [self enterMainAction];
    }
}

// 不會自動停止, 需要用戶點擊按鈕才能進入應用
- (void)createLoginBtn1{ // 這里的時間是3秒后視頻頁面出現(xiàn)按鈕
    _timer1 = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(showClickBtn) userInfo:nil repeats:YES];
}
- (void)showClickBtn{
    NSLog(@"顯示進入應用按鈕");
    UIButton *btn = [[UIButton alloc] init];
    btn.frame = CGRectMake(30, SCREEN_HEIGHT - 100, SCREEN_WIDTH - 60, 40);
    btn.backgroundColor = [UIColor redColor];
    btn.layer.cornerRadius = 20;
    btn.alpha = 0.5;
    [btn setTitle:@"進入應用" forState:UIControlStateNormal];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(enterMainAction) forControlEvents:UIControlEventTouchUpInside];
    [_timer1 invalidate];
    _timer1 = nil;// timer置為nil

}
- (void)enterMainAction{
    NSLog(@"點擊了進入應用按鈕");
    ViewController *vc = [[ViewController alloc]init];
    self.view.window.rootViewController = vc;
    [self.AVPlayer.player pause];
}
@end

Appdelegate里的調(diào)用:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    // 這是圖片還有視頻的url鏈接
    NSString *getUrlStr = @"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"; // 網(wǎng)絡視頻
//    NSString *getUrlStr = @"http://fimg.yucuizhubao.com/img/start.png"; // 網(wǎng)絡圖片
    
    NSLog(@"后綴是--%@", [getUrlStr substringFromIndex:[getUrlStr length] - 4]);
    ZHMoviePlayerController *ZHVC = [[ZHMoviePlayerController alloc]init];
    
    if ([[getUrlStr substringFromIndex:[getUrlStr length] - 4] isEqualToString:@".mp4"] ) {
        NSLog(@"加載的是視頻");
//        [ZHVC setMoviePlayerInIndexWithURL:[NSURL URLWithString:getUrlStr] localMovieName:nil]; // 加載網(wǎng)絡url視頻
        [ZHVC setMoviePlayerInIndexWithURL:nil localMovieName:@"movie.mp4"]; // 加載本地視頻
        self.window.rootViewController = ZHVC;
        
    }else if ([[getUrlStr substringFromIndex:[getUrlStr length] - 4] isEqualToString:@".png"]){
        NSLog(@"加載的是圖片");
//        [ZHVC setImageInIndexWithURL:[NSURL URLWithString:getUrlStr] localImageName:nil timeCount:4];// 加載網(wǎng)絡圖片
        [ZHVC setImageInIndexWithURL:nil localImageName:@"bj.png" timeCount:4]; // 加載本地圖片
        self.window.rootViewController = ZHVC;
    }
    return YES;
}

作者:Haleszh
鏈接:http://www.reibang.com/p/240032c245ec
來源:簡書
著作權歸作者所有蛮穿。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權,非商業(yè)轉(zhuǎn)載請注明出處娇未。

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末诬烹,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子幻馁,更是在濱河造成了極大的恐慌绍填,老刑警劉巖掌测,帶你破解...
    沈念sama閱讀 218,607評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件内贮,死亡現(xiàn)場離奇詭異,居然都是意外死亡汞斧,警方通過查閱死者的電腦和手機夜郁,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來粘勒,“玉大人竞端,你說我怎么就攤上這事∶硭” “怎么了事富?”我有些...
    開封第一講書人閱讀 164,960評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長乘陪。 經(jīng)常有香客問我统台,道長,這世上最難降的妖魔是什么啡邑? 我笑而不...
    開封第一講書人閱讀 58,750評論 1 294
  • 正文 為了忘掉前任贱勃,我火速辦了婚禮,結(jié)果婚禮上谤逼,老公的妹妹穿的比我還像新娘贵扰。我一直安慰自己,他們只是感情好流部,可當我...
    茶點故事閱讀 67,764評論 6 392
  • 文/花漫 我一把揭開白布戚绕。 她就那樣靜靜地躺著,像睡著了一般枝冀。 火紅的嫁衣襯著肌膚如雪舞丛。 梳的紋絲不亂的頭發(fā)上耘子,一...
    開封第一講書人閱讀 51,604評論 1 305
  • 那天,我揣著相機與錄音瓷马,去河邊找鬼拴还。 笑死,一個胖子當著我的面吹牛欧聘,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播端盆,決...
    沈念sama閱讀 40,347評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼怀骤,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了焕妙?” 一聲冷哼從身側(cè)響起蒋伦,我...
    開封第一講書人閱讀 39,253評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎焚鹊,沒想到半個月后痕届,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,702評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡末患,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,893評論 3 336
  • 正文 我和宋清朗相戀三年研叫,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片璧针。...
    茶點故事閱讀 40,015評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡嚷炉,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出探橱,到底是詐尸還是另有隱情申屹,我是刑警寧澤,帶...
    沈念sama閱讀 35,734評論 5 346
  • 正文 年R本政府宣布隧膏,位于F島的核電站哗讥,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏胞枕。R本人自食惡果不足惜杆煞,卻給世界環(huán)境...
    茶點故事閱讀 41,352評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望曲稼。 院中可真熱鬧索绪,春花似錦、人聲如沸贫悄。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,934評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽窄坦。三九已至唤反,卻和暖如春凳寺,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背彤侍。 一陣腳步聲響...
    開封第一講書人閱讀 33,052評論 1 270
  • 我被黑心中介騙來泰國打工肠缨, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人盏阶。 一個月前我還...
    沈念sama閱讀 48,216評論 3 371
  • 正文 我出身青樓晒奕,卻偏偏與公主長得像,于是被迫代替她去往敵國和親名斟。 傳聞我的和親對象是個殘疾皇子脑慧,可洞房花燭夜當晚...
    茶點故事閱讀 44,969評論 2 355