视频截图和重力旋转

1111话不多少,直接上代码

#import "ViewController.h"
#import 
#import 

#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)coordinator {
    [coordinator animateAlongsideTransition:^(id  _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  _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"];
}

你可能感兴趣的:(视频截图和重力旋转)