Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 有没有办法在AVPlayerLayer中捕获视图的截图?_Ios_Objective C_Uiimage_Avfoundation_Avplayerlayer - Fatal编程技术网

Ios 有没有办法在AVPlayerLayer中捕获视图的截图?

Ios 有没有办法在AVPlayerLayer中捕获视图的截图?,ios,objective-c,uiimage,avfoundation,avplayerlayer,Ios,Objective C,Uiimage,Avfoundation,Avplayerlayer,我目前正在制作一个视频拼贴应用程序。我有一个视图将Avplayerlayer作为子层包含。我需要得到一个包含AvplayerLayer的视图的屏幕截图。当我尝试拍摄它时,它得到了屏幕截图,但是对于avplayerlayer(视频正在里面播放)来说,它不在屏幕截图中,只是一个黑屏。因为模拟器是完美的工作和显示层也为真正的设备只是一个黑屏 我在StackOverFlow和appleds的开发人员文档中尝试了所有的解决方案,但没有任何效果 我尝试过的一些解决方案: 正如您在我的代码中看到的,它应

我目前正在制作一个视频拼贴应用程序。我有一个视图将Avplayerlayer作为子层包含。我需要得到一个包含AvplayerLayer的视图的屏幕截图。当我尝试拍摄它时,它得到了屏幕截图,但是对于avplayerlayer(视频正在里面播放)来说,它不在屏幕截图中,只是一个黑屏。因为模拟器是完美的工作和显示层也为真正的设备只是一个黑屏

我在StackOverFlow和appleds的开发人员文档中尝试了所有的解决方案,但没有任何效果

我尝试过的一些解决方案:

正如您在我的代码中看到的,它应该可以从视图中获取图像,但对于avplayerlayer则不起作用

- (UIImage *)imageFromView:(UIView *)view
{
   UIGraphicsBeginImageContext(view.frame.size);

    [view drawViewHierarchyInRect:_videoFrame afterScreenUpdates:false];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    NSString *fileExtension = @"png";

    NSData *data;

    Boolean *isOutputJPS = false;

    if (isOutputJPS) {
        data = UIImageJPEGRepresentation(image, 0.5);
        fileExtension = @"jpg";
    }else{
        data = UIImagePNGRepresentation(image);
    }

    UIImage  *rasterizedView = [UIImage imageWithData:data];
    UIGraphicsEndImageContext();
    return rasterizedView;
}

//in the viewController

UIImage *image =  [self imageFromView:recordingView];
我现在有点绝望,因为Avplayerlayer没有任何解决方案。
当我检查真实设备中生成的图像时,它只是向我显示视图,但对于模拟器来说,它的工作方式与我预期的一样。

有很多方法可以实现您想要做的事情。我发现使用资产图像生成器总是有效的

- (NSImage *)getImageFromAsset:(AVAsset *)myAsset width:(int)theWidth height:(int)theHeight {

                            Float64 durationSeconds = CMTimeGetSeconds(myAsset.duration);

    /// Change frametimetoget section to your specific needs ///
                            CMTime frametimetoget;
                            if (durationSeconds <= 20) {
                                            frametimetoget = CMTimeMakeWithSeconds(durationSeconds/2, 600);
                            } else {
                                            frametimetoget = CMTimeMakeWithSeconds(10, 600);
                            }

                            AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:myAsset];
                            imageGenerator.appliesPreferredTrackTransform = YES;
                            imageGenerator.maximumSize = CGSizeMake(theWidth, theHeight);
                            NSString *aspect = @"AVAssetImageGeneratorApertureModeEncodedPixels";
                            imageGenerator.apertureMode = aspect;

    /// NSError not handled in this example , you would have to add code ///
                            NSError *error = nil;
                            CMTime actualTime;
                            CGImageRef frameImage = [imageGenerator copyCGImageAtTime:frametimetoget actualTime:&actualTime error:&error];

                            Float64 myImageWidth = CGImageGetWidth(frameImage);
                            Float64 myImageHeight = CGImageGetHeight(frameImage);
                            Float64 ratio = myImageWidth/theWidth;
                            NSSize imageSize ;
                            imageSize.width=myImageWidth/ratio;
                            imageSize.height=myImageHeight/ratio;

    /// You may choose to use CGImage and skip below
    /// Swap out NSImage (Mac OS x) for the ios equivalence
                            NSImage * thumbNail = [[NSImage alloc]initWithCGImage:frameImage size:imageSize];

    /// CGImageRelease is a must to avoid memory leaks
                            CGImageRelease(frameImage);
                            return thumbNail;
-(NSImage*)从资产获取图像:(AVAsset*)我的资产宽度:(int)宽度高度:(int)高度{
Float64 durationSeconds=CMTimeGetSeconds(myAsset.duration);
///根据您的具体需要更改frametimetoget部分///
CMTime-frametimetoget;
如果(持续时间)秒