我试图截屏播放视频,但屏幕截图始终显示为空白。这是我的代码:
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播放视频。
你们中的任何人都知道为什么会这样吗?或者如果我需要对视频做一些特别的事情来拍摄截图?
我非常感谢你的帮助。
答案 0 :(得分:0)
我找到了解决问题的方法:
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];