iOS:撷取萤幕撷取画面以呈现影片ipad [英] iOS: taking screenshot presenting video ipad
本文介绍了iOS:撷取萤幕撷取画面以呈现影片ipad的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试截取播放视频的屏幕截图,但屏幕截图始终显示为空白.这是我的代码:
I'm trying to take screenshot playing video but the screenshot always is showing blank. This is my code:
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *data = UIImageJPEGRepresentation(viewImage, 1.0);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"image.jpg"];
[data writeToFile:filePath atomically:YES];
我正在使用AVPlayer/AVPlayerLayer播放视频.
I'm using AVPlayer/AVPlayerLayer to play the video.
你们中的任何人都知道为什么会这样吗?还是我需要对视频做一些特殊的事情才能拍摄屏幕截图?
Any of you knows why this happening? or if I need to do something special with video to be able to take the screenshot?
非常感谢您的帮助.
推荐答案
我找到了解决问题的方法:
I found a solution to my problem:
AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:[self.player.currentItem asset]];
CGImageRef ref = [imageGenerator copyCGImageAtTime:self.player.currentItem.currentTime actualTime:nil error:nil];
UIImage *viewImage = [UIImage imageWithCGImage:ref];
CGImageRelease(ref);
NSData *data = UIImageJPEGRepresentation(viewImage, 1.0);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"image.jpg"];
[data writeToFile:filePath atomically:YES];
这篇关于iOS:撷取萤幕撷取画面以呈现影片ipad的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文