如何在iOS 7(越狱)中对整个屏幕进行截图,无论前台运行的是哪个应用程序

14
在iOS7之前,我使用UIGetScreenImage()函数轻松地截取屏幕截图,但在iOS7中,它已经过时了。现在有没有好的方法来实现这个功能呢?谢谢!
补充说明:我需要在任何视图下对整个屏幕进行截图。

1
我认为这可能会有所帮助: https://dev59.com/KGMk5IYBdhLWcg3wyw_H - P. Sami
@P.Sami,@SamkitJain,感谢你们的回答,但是它不能解决我的问题。我的应用程序是一个运行在“SpringBoard”内部的越狱插件应用程序,我想在任何应用程序的任何视图顶部捕获屏幕截图,包括主屏幕,而不仅仅是我的应用程序的视图。我该怎么办? - Suge
@Suge,你看一下这个重复的问题并阅读它的答案。越狱不是黑魔法(嗯,...),插件可以访问与官方应用程序相同的API。 - user529758
@H2CO3,使用[UIWindow mainWindow]我只能获取到SpringBoard的视图,而无法获取到最前面的应用程序。 - Suge
你可以使用以下方式:在此输入链接说明在此输入链接说明 - Alexander Merchi
显示剩余2条评论
4个回答

4

我遇到了同样的问题,但不知道该如何解决。

我尝试使用IOSurface - IOS Private API - Capture screenshot in background,在一些应用中效果很好,但在游戏中却返回黑屏。

然后我尝试了这个应用程序https://github.com/k06a/UIView-FastScreenshot/blob/master/UIView%2BFastScreenshot.m,它使用私有API,效果很好,但我无法使用theos编译它,总是告诉我“Undefined symbols for architecture armv7: CARenderServerRenderDisplay”。 编辑:我弄清楚了如何使用theos编译它,但返回一个空图像。

https://github.com/coolstar/RecordMyScreen在iOS7上效果很好,但现在它不是开源的,所以我找不到它是如何捕获整个屏幕的。

编辑:RecordMyScreen的代码作为springboard tweak在iOS7上工作,您可以参考这个文件https://github.com/coolstar/RecordMyScreen/blob/master/RecordMyScreen/CSScreenRecorder.m这个方法“- (void)_captureShot:(CMTime)frameTime;”


谢谢您的回答,RecordMyScreen现在是开源的吗?代码看起来有点老旧。 - Suge
RecordMyScreen现在不是开源的,但是通过最新版本的一些反向工程,截屏的代码似乎没有太大变化。此外,您可以参考这个分支https://github.com/Norod/RecordMyScreen,它适用于iOS7。 - cloudycliff
我上传了我的调整 https://github.com/cloudycliff/CaptureMyScreen ,当激活时它将把屏幕截图保存到 /var/mobile/Documents/test.png - cloudycliff
@tesmojones 代码由Activator激活,您可以在设置中进行设置。或者您可以将捕获代码直接添加到您的调整中。 - cloudycliff
在Norod版本中,当它从后台返回以便我停止录制时,它一直崩溃并显示“收到内存警告”。此时没有其他应用程序在运行。有什么想法吗? - EmilyJ

1

替代UIGetScreenImage()的另一种方法

CGImageRef screen = UIGetScreenImage();

上述代码是用于截屏的一行代码,但是苹果并未向公共应用开放上述API UIGetScreenImage(),也就是说,无法上传到苹果进行审核。因此,以下列出了替代的截屏和保存方法。
- (void)captureAndSaveImage
{    

    // Capture screen here... and cut the appropriate size for saving and uploading
    UIGraphicsBeginImageContext(self.view.bounds.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // crop the area you want 
    CGRect rect;
    rect = CGRectMake(0, 10, 300, 300);    // whatever you want
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
    UIImage *img = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef);
    UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    imageView.image = img; // show cropped image on the ImageView     
}

// this is option to alert the image saving status
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    UIAlertView *alert;

    // Unable to save the image  
    if (error)
        alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                                           message:@"Unable to save image to Photo Album." 
                                          delegate:self cancelButtonTitle:@"Dismiss" 
                                 otherButtonTitles:nil];
    else // All is well
        alert = [[UIAlertView alloc] initWithTitle:@"Success" 
                                           message:@"Image saved to Photo Album." 
                                          delegate:self cancelButtonTitle:@"Ok" 
                                 otherButtonTitles:nil];
    [alert show];
    [alert release];
}

以下是一些链接,可以帮助你合法替换 UIGetScreenImage


1
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

这段代码可以帮助您获取iPhone屏幕上的整个界面截图,无论前台运行哪个应用程序。如果您在视图中使用了任何图层,则这些图层不会包含在截图中,您需要使用self.view.layer。

0

UIView类有一个名为resizableSnapshotViewFromRect:afterScreenUpdates:withCapInsets的方法。

但我从未使用过这个方法,所以无法告诉您更多信息。

而这篇文章:将UIView捕获为UIImage展示了如何将返回的UIView转换为图像。

希望这对您有用。


请使用Samkit Jain的答案。 - Mirko Brunner
1
谢谢您的回答,但它不能解决我的问题。我的应用是一个运行在SpringBoard内的越狱插件应用程序,我想在任何应用程序的任何视图顶部捕获屏幕截图,包括主屏幕,而不仅仅是我的应用程序的视图。我该怎么办? - Suge

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接