0

我正在尝试截屏播放视频,但屏幕截图始终显示为空白。这是我的代码:

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 播放视频。

你们中的任何人都知道为什么会发生这种情况吗?或者如果我需要对视频做一些特别的事情才能截取屏幕截图?

我会非常感谢你的帮助。

4

1 回答 1

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];
于 2014-11-14T19:57:18.857 回答