【问题标题】:Snap shot is not working?快照不起作用?
【发布时间】:2015-02-23 03:35:22
【问题描述】:

我是移动编程的新手。我正在使用 VideoToolBox 框架在 iOS 应用程序中进行 H264 视频渲染。它具有在渲染视频时拍摄快照的一项功能。每当我拍摄快照时,我只会得到黑屏。 我试过这个 1. 渲染上下文, 2.drawViewHierarchyInRect, 3. snapshotViewAfterScreenUpdates 方法 捕获渲染视频,但仅返回黑屏。

//snapshot coding
UIGraphicsBeginImageContextWithOptions (self.view.bounds.size, YES, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
mImageView.image = snapshotImage;
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(snapshotImage,self, @selector(image:didFinishSavingWithError: contextInfo:), nil);

【问题讨论】:

  • 以上代码我用来从 IOS 8.1 拍摄快照

标签: ios objective-c xcode snapshot


【解决方案1】:

看看这个, 以下代码块可以让我拍摄屏幕快照

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
    UIGraphicsBeginImageContextWithOptions(APP_DELEGATE.window.bounds.size, NO, [[UIScreen mainScreen] scale]);
else
    UIGraphicsBeginImageContext(APP_DELEGATE.window.bounds.size);

[APP_DELEGATE.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);

我想,它会帮助你。如果有请告诉我

【讨论】:

  • 谢谢你的朋友,但这段代码在视频渲染到快照中不起作用。再次感谢。 IOS 8.1 错误
【解决方案2】:

我还没有处理过视频,但是 UIView 的简单快照和子视图可以正常工作

+ (UIImage *)makeSnapShot:(UIView *)view image:(UIImageView *)imageView
{
    CGFloat offset_x = /*your_value*/;
    CGFloat offset_y = /*your_value*/;
    UIGraphicsBeginImageContext(view.bounds.size);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    CGRect rect = CGRectMake(offset_x, offset_y, imageView.bounds.size.width, imageView.bounds.size.height);

    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
    image = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);

    return image;
}

【讨论】:

  • 谢谢你的朋友,但是这段代码在视频渲染到快照中不起作用。再次感谢。 IOS 8.1 错误
【解决方案3】:

不确定这是否是您要查找的内容,但如果您需要获取 VTDecompressionSession 的快照,您可以将从 decodeFrame 回调中获取的 CVImageBuffer 发送到此方法中以获取 UIImage。您还可以将 CIContext 添加到参数列表中,而不是使用temporaryContext。

+ (UIImage *) UIImageFromCVImageBufferRef:(CVImageBufferRef)imageBuf
{
    CIImage *ciImage = [CIImage imageWithCVPixelBuffer:imageBuf];
    CIContext *temporaryContext = [CIContext contextWithOptions:nil];
    CGImageRef videoImage = [temporaryContext
                             createCGImage:ciImage
                             fromRect:CGRectMake(0, 0,
                                                 CVPixelBufferGetWidth(imageBuf),
                                                 CVPixelBufferGetHeight(imageBuf))];
    UIImage *image = [[UIImage alloc] initWithCGImage:videoImage];
    CGImageRelease(videoImage);
    return image;
}

【讨论】:

    【解决方案4】:
    func takeScreenshot(_ shouldSave: Bool = true) {
           var screenshotImage :UIImage?
           let layer = UIApplication.shared.keyWindow!.layer
           let scale = UIScreen.main.scale
           UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale)
        self.view.drawHierarchy(in: self.view.bounds, afterScreenUpdates: true)
           screenshotImage = UIGraphicsGetImageFromCurrentImageContext()
           UIGraphicsEndImageContext()
           if let image = screenshotImage, shouldSave {
               UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
           }
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-27
      • 2012-06-11
      • 2016-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-07
      • 1970-01-01
      相关资源
      最近更新 更多