视频截图和重力旋转

1111话不多少,直接上代码

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

#define ScreenWidth  [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
@interface ViewController ()

@property(nonatomic,strong)AVPlayerItem *playerItem;
@property(nonatomic,strong)AVPlayerItemVideoOutput *PlayerItemVideoOutput;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property(nonatomic,assign) BOOL shouldRotate;
@property(nonatomic,strong) AVPlayer* player;
@property(nonatomic,strong) AVPlayerViewController *playerVC;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.shouldRotate = YES;
    //本地播放
//    NSString *path = [[NSBundle mainBundle] pathForResource:@"video4" ofType:@"mp4"];
//
//    
//    if (path) {
//        NSURL *sourceMovieURL = [NSURL fileURLWithPath:path];
//        AVAsset *movieAsset = [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil];
//        self.playerItem = [AVPlayerItem playerItemWithAsset:movieAsset];
//    }

    //在线播放
    NSMutableString *filepath = [[NSMutableString alloc]initWithString:[NSString stringWithFormat:@"http://hls.quanmin.tv/live/44397/playlist.m3u8"]];
    NSURL *url = [NSURL URLWithString:filepath];
    self.playerItem = [AVPlayerItem playerItemWithURL:url];
    self.PlayerItemVideoOutput = [[AVPlayerItemVideoOutput alloc] init];
    [self.playerItem addOutput:self.PlayerItemVideoOutput];

    
    AVPlayer *player = [AVPlayer playerWithPlayerItem:_playerItem];
    _playerVC = [[AVPlayerViewController alloc]init];
    _playerVC.showsPlaybackControls = NO;
    _playerVC.player = player;
    [_playerVC setVideoGravity:AVLayerVideoGravityResize];
    _playerVC.view.frame = CGRectMake(0, 20, ScreenWidth, ScreenWidth*9/16);
    
    
    [player play];
    [self.view addSubview:_playerVC.view];
    
}
- (BOOL)shouldAutorotate
{
    return self.shouldRotate;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    switch (toInterfaceOrientation) {
        case UIInterfaceOrientationLandscapeLeft:
            NSLog(@"left");
            [self landscape];
            break;
        case UIInterfaceOrientationLandscapeRight:
            NSLog(@"right");
            [self landscape];
            break;
        case UIInterfaceOrientationPortrait:
            NSLog(@"portrait");
            [self portrait];
            break;
        default:
            break;
    }
}

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
        switch (orientation) {
            case UIInterfaceOrientationLandscapeLeft:
                NSLog(@"left");
                [self landscape];
                break;
            case UIInterfaceOrientationLandscapeRight:
                NSLog(@"right");
                [self landscape];
                break;
            case UIInterfaceOrientationPortrait:
                NSLog(@"portrait");
                [self portrait];
                break;
            default:
                break;
        }
    } completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        
    }];
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}

- (void)portrait {
    
    _playerVC.view.frame = CGRectMake(0, 20, ScreenWidth, ScreenWidth*9/16);
    

}
- (void)landscape {
    
    _playerVC.view.frame = self.view.bounds;
}
//截图
- (IBAction)takeaphoto:(UIButton *)sender {

    CMTime itemTime = self.playerItem.currentTime;
    
    CVPixelBufferRef pixelBuffer = [self.PlayerItemVideoOutput copyPixelBufferForItemTime:itemTime itemTimeForDisplay:nil];
    NSLog(@"%@",pixelBuffer);//为空
        CIImage *ciImage = [CIImage imageWithCVPixelBuffer:pixelBuffer];
        CIContext *temporaryContext = [CIContext contextWithOptions:nil];
        CGImageRef videoImage = [temporaryContext
                                 createCGImage:ciImage
                                 fromRect:CGRectMake(0, 0,
                                                     CVPixelBufferGetWidth(pixelBuffer),
                                                     CVPixelBufferGetHeight(pixelBuffer))];
        
        UIImage *frameImg = [UIImage imageWithCGImage:videoImage];
        
        CGImageRelease(videoImage);
        self.imageView.image = frameImg;

}

@end

上面是写swift的demo
实际项目中在Objective_C中
可能有时候我们有全屏按钮,或者返回按钮,这时我们就能用到下面 的方法了

//点击按钮旋转到横屏
- (void)switchToLandscape
{
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];
    //上一句话是防止手动先把设备置为横屏,导致下面的语句失效.
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];
}

//点击返回旋转到竖屏
- (void)switchToPortrait
{
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];
    //上一句话是防止手动先把设备置为竖屏,导致下面的语句失效.
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,250评论 4 61
  • 2017-04-19 华杉 儒家很大程度上是明哲保身,妥协的艺术,王阳明则是奋不顾身,绝无妥协。 【答聂文蔚 春间...
    郁萍阅读 1,011评论 0 0
  • 1.获取Android源码 (1)下载repo 在用户目录下创建一个bin文件夹来存放repo,并把该路径设置到环...
    孙肖依阅读 10,038评论 1 12