笔记--9.10 (抓屏和文件路径初始化NSURL)

本文介绍如何在iOS应用中使用NSURL及MPMoviePlayerController播放指定路径的mp4视频,并实现异步截图功能。通过监听特定通知来触发截图完成后的处理流程。

1,使用文件路径初始化NSURL对象

 NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"mp4"];
 NSURL *url = [NSURL fileURLWithPath:path];
 moviePlay = [[MPMoviePlayerController alloc] initWithContentURL:url];

2,异步抓屏

a,首先添加观察者,并实现观察到后的处理方法。

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didReceived:) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:nil];

b,调用异步抓屏方法,抓取完毕后,发送相应通知。

[movieViewCtrl.moviePlayer  requestThumbnailImagesAtTimes:array timeOption:MPMovieTimeOptionExact];
[[NSNotificationCenter  defaultCenter] postNotificationName:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:nil];


c,处理方法的具体实现

- (void)didReceived:(NSNotification *)notif
{
    UIImage *catchPic = [notif.userInfo objectForKey:MPMoviePlayerThumbnailImageKey];
    //采用switch并不好,因为switch的参数只能是整数。
    if (catchPic) {
        switch ([[notif.userInfo objectForKey:MPMoviePlayerThumbnailTimeKey] intValue]) {
            case 2:
                self.catchImageView.image = catchPic;
                [self.view addSubview:_catchImageView];
                break;
            case 8:
                self.secondCatchImageView.image = catchPic;
                [self.view addSubview:_secondCatchImageView];
                break;
            default:
                break;
        }
    }
    
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值