Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.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
Iphone 如何在iOS中以编程方式拍摄屏幕截图?_Iphone_Ios_Opengl Es_Uikit_Screenshot - Fatal编程技术网

Iphone 如何在iOS中以编程方式拍摄屏幕截图?

Iphone 如何在iOS中以编程方式拍摄屏幕截图?,iphone,ios,opengl-es,uikit,screenshot,Iphone,Ios,Opengl Es,Uikit,Screenshot,我有一个UIView,它使用UIKit控件和OpenGL。我想通过编程获得该视图的屏幕截图 如果使用UIGraphicsGetImageFromCurrentImageContext(),则OpenGL内容为空; 如果使用glReadPixels(…)方法,UIKit内容为空 我对如何拍摄完整的截图感到困惑。 谢谢大家! 以下是如何以编程方式拍摄UIImage的简单屏幕截图。如果要将其用于任何其他对象,请使用它替换UIImage 在.h文件中添加NSData*数据然后在.m文件中将其添加到代码中

我有一个
UIView
,它使用
UIKit
控件和
OpenGL
。我想通过编程获得该视图的屏幕截图

如果使用
UIGraphicsGetImageFromCurrentImageContext()
,则
OpenGL
内容为空; 如果使用
glReadPixels(…)
方法,UIKit内容为空

我对如何拍摄完整的截图感到困惑。
谢谢大家!

以下是如何以编程方式拍摄
UIImage
的简单屏幕截图。如果要将其用于任何其他对象,请使用它替换
UIImage

.h
文件中添加
NSData*数据
然后在
.m
文件中将其添加到代码中

UIGraphicsBeginImageContext(self.view.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *attachimage = [[UIImage alloc]initWithData:Data];
UIImage *viImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
attachimage = viImage;
UIImageWriteToSavedPhotosAlbum(viImage, nil, nil, nil);

您可以在不损失图像质量的情况下获得图像结果

    - (UIImage *)captureView:(UIView *)view {
     UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
     [view.layer renderInContext:UIGraphicsGetCurrentContext()];
     UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();
     return img;
     }

您知道OpenGL内容在视图中的位置。因此,将
glReadPixels
的结果复制到UIKit的快照中应该很容易。

也许。。你能用这里的东西吗

在这里


使用以下代码拍摄屏幕截图

    -(UIImage *) screenshot
     {

          CGRect rect;
          rect=CGRectMake(0, 0, 320, 480);
          UIGraphicsBeginImageContext(rect.size);

          CGContextRef context=UIGraphicsGetCurrentContext();
          [self.view.layer renderInContext:context];

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

          return image;
     }

好吧,有几种方法可以通过编程捕捉iPhone屏幕

  • 使用UIKIT

  • 使用avfoundationframework

  • 使用opengles

  • 从iOS 7开始,您还可以使用

  • 使用UIWindow

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    UIGraphicsBeginImageContext(screenRect.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [[UIColor blackColor] set];
    CGContextFillRect(ctx, screenRect);
    [self.window.layer renderInContext:ctx];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
  • 使用UIView

    UIGraphicsBeginImageContext(self.view.frame.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viImage=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

  • 在这6个选项中,我发现第一个选项对于复制粘贴和应用压缩级别非常方便,因为第一个方法提供了具有真实像素数据的图像。我也喜欢选项4,因为API附带SDK。

    它在OpenGL页面中不起作用,它只在UIKit条件下起作用,对于OpenGL页面,只获取空白图像它只在UIKit条件下起作用,对于OpenGL页面,只获取空白图像我正在开发一个屏幕截图控件,我不知道该视图是由UIKit还是OpenGL实现的,或者混合。如果我能得到视图由什么实现的信息,问题就会解决。@coodi8我认为这可能是个问题。这并不容易,因为OpenGL直接在图形芯片上渲染。UI图形不在图形芯片上呈现。(AFAIK)感谢这一点,似乎带有子视图控制器的屏幕不会自动添加子视图控制器层。但是使用窗口似乎对全屏图像有效。谢谢有人能解释一下它是怎么工作的吗?另外还需要修正方向,但这段代码只是针对take UIKit的东西…问题更复杂,并且解释得很清楚:他还需要使用OpenGL的东西。
    -(UIImage *) screenshot
    {
    CGImageRef UIGetScreenImage(void);
    CGImageRef screen = UIGetScreenImage();
    UIImage* screenImage = [UIImage imageWithCGImage:screen];
    CGImageRelease(screen); // you need to call this.
    return screenImage;
    }
    
    -(UIImage *) screenshot
    {
    CGImageRef UIGetScreenImage(void);
    CGImageRef screen = UIGetScreenImage();
    UIImage* screenImage = [UIImage imageWithCGImage:screen];
    CGImageRelease(screen); // you need to call this.
    return screenImage;
    }