iOS 截屏的总结,AVplayer的截屏,当前屏幕的截屏,截屏后导航栏不见的情况

转载自: http://www.jianshu.com/p/8e9234a70955

 
 

1.截取当前屏幕,导航栏不见。

方法:

- (UIImage*)screenView:(UIView *)view{

UIGraphicsBeginImageContextWithOptions(CGSizeMake(CGRectGetWidth(view.frame), CGRectGetHeight(view.frame)), NO, 1);

[view drawViewHierarchyInRect:CGRectMake(0, 0, CGRectGetWidth(view.frame), CGRectGetHeight(view.frame)) afterScreenUpdates:NO];

UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return snapshot;

}

使用:

UIImage *image = [self screenView: [UIApplication sharedApplication].keyWindow ];

UIImageView *snapView = [[UIImageView alloc] initWithImage:image];

snapView.alpha = 1.0f;

snapView.frame = [UIApplication sharedApplication].keyWindow.frame;

2.AVplay 视频流的截屏

发现视频流的截屏是黑色,而不是当前帧的画面

#pragma mark - 截图 点击事件 方法

- (IBAction)didClickSnip:(UIBarButtonItem *)sender {

self.imgGenerator = [[AVAssetImageGenerator alloc]initWithAsset:self.playerItem.asset];

self.imgGenerator.requestedTimeToleranceBefore = kCMTimeZero;

self.imgGenerator.requestedTimeToleranceAfter = kCMTimeZero;

//第一种 --截图方法

//    CGImageRef imgRef = [self.imgGenerator copyCGImageAtTime:self.player.currentTime actualTime:NULL error:nil];

//

//    self.snipImageView.image = [UIImage imageWithCGImage:imgRef];

//第二种 --截图方法

NSValue *value = [NSValue valueWithCMTime:self.player.currentTime];

[self.imgGenerator generateCGImagesAsynchronouslyForTimes:@[value] completionHandler:^(CMTime requestedTime, CGImageRef  _Nullable image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError * _Nullable error) {

//这里先用OC对象对C变量强引用,保证在回到主线程中不会被释放掉

UIImage *img = [UIImage imageWithCGImage:image];

dispatch_async(dispatch_get_main_queue(), ^{

self.snipImageView.image = img;

});

}];

}

3.AVplay 获取当前帧图片

/**

Description

获取当前帧图片

@return return value description

*/

//获取输出流的地址

@property(nonatomic, strong) AVPlayerItemVideoOutput * videoOutPut;

-(UIImage*)getScreenShotImg{

CMTime time = [self.videoOutPut itemTimeForHostTime:CACurrentMediaTime()];

if (0 == time.value) {

return nil;

}

//截取到对应的图片

if ([self.videoOutPut hasNewPixelBufferForItemTime:time]) {

CVPixelBufferRef lastSnapshotPixelBuffer = [self.videoOutPut copyPixelBufferForItemTime:time itemTimeForDisplay:NULL];

CIImage *ciImage = [CIImage imageWithCVPixelBuffer:lastSnapshotPixelBuffer];

CIContext *context = [CIContext contextWithOptions:NULL];

CGRect rect = CGRectMake(0,

0,

CVPixelBufferGetWidth(lastSnapshotPixelBuffer),

CVPixelBufferGetHeight(lastSnapshotPixelBuffer));

CGImageRef cgImage = [context createCGImage:ciImage fromRect:rect];

self.screenShotImg = [UIImage imageWithCGImage:cgImage];

//当前帧的画面

CGImageRelease(cgImage);

return self.screenShotImg;

}

return nil;

}

作者:白色的黑豹1 链接:http://www.jianshu.com/p/8e9234a70955 來源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值