快照不起作用?

时间:2014-12-25 11:15:19

标签: ios objective-c xcode snapshot

我是移动节目的新手。我正在使用VideoToolBox框架在iOS应用程序中使用H264视频渲染。它有一个功能,可以在渲染视频时拍摄快照。每当我拍摄快照时,我只会看到黑屏。 我试过这个 1. renderInContext, 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);

4 个答案:

答案 0 :(得分: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);

我想,它会对你有所帮助。如果是的话,请告诉我

答案 1 :(得分:1)

我还没有使用过视频,但是带有子视图的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;
}

答案 2 :(得分:1)

不确定这是否是您正在寻找的,但是如果您需要获取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;
}

答案 3 :(得分:0)

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)
       }
   }