在ios7中制作整个屏幕的截图

时间:2014-01-28 19:34:51

标签: ios objective-c ios7 screenshot theos

我正在尝试从使用THEOS编译的命令行工具中截取整个屏幕的屏幕截图。 我已经尝试了所有私有API UICreateScreenImage,UIGetScreenImage和_UICreateScreenUIImage,都返回黑色或空白图像(但图片的大小是正确的)

这是我使用“_UICreateScreenUIImage”API的代码:

    extern "C" UIImage* _UICreateScreenUIImage();

    int main(int argc, char **argv, char **envp) {

        UIImage* screenImage = _UICreateScreenUIImage();

        [UIImagePNGRepresentation(screenImage) writeToFile:@"/var/mobile/screenshot.png" atomically:YES];

        [screenImage release];
        return 0;
    }

我已将框架链接起来:将UIKit QuartzCore CoreGraphics和GraphicsServices作为私有框架。 我不能使用此处描述的Apple方式Why does my programmatically created screenshot look so bad on iOS 7?,因为它是一个命令行工具。 有什么建议吗?

1 个答案:

答案 0 :(得分:1)

你可以试试我的代码。它对我有用。

选项1:

- (void)takeScreenshotAndSaveToPhotosAlbum {
    extern UIImage *_UICreateScreenUIImage();
    UIImage *screenshot = _UICreateScreenUIImage();
    UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil);
}

选项2:

typedef struct __IOSurface *IOSurfaceRef;
UIKIT_EXTERN CGImageRef UICreateCGImageFromIOSurface(IOSurfaceRef);

- (void)takeScreenshotAndSaveToPhotosAlbum {
    IOSurfaceRef ioSurfaceRef = (__bridge IOSurfaceRef)([UIWindow performSelector:@selector(createScreenIOSurface)]);
    CGImageRef cgImageRef = UICreateCGImageFromIOSurface(ioSurfaceRef);
    UIImage *uiImage = [UIImage imageWithCGImage:cgImageRef];
    CFRelease(ioSurfaceRef);
    CGImageRelease(cgImageRef);
    UIImageWriteToSavedPhotosAlbum(uiImage, nil, nil, nil);
}