How to take screenshot of entire screen on a Jailb

2019-01-29 09:23发布

问题:

I need to take screenshot of the whole screen including the status bar, I use CARenderServerRenderDisplay to achieve this, it works right on iPad, but wrong at iPhone 6 Plus. As the * marked part in the code, if I set width=screenSize.width*scale and height=screenSize.height*scale, it will cause crash, if I just change them as:width=screenSize.height*scale and height=screenSize.width*scale, it will works, but produce a image like that: , I've tried much but no reason found, does anyone know that? I hope I've described it clear enough.

- (void)snapshot
{
    CGFloat scale = [UIScreen mainScreen].scale;
    CGSize screenSize = [UIScreen mainScreen].bounds.size;

    //*********** the place where problem appears        
    size_t width = screenSize.height * scale;
    size_t height = screenSize.width * scale;
    //***********

    size_t bytesPerElement = 4;
    OSType pixelFormat = 'ARGB';
    size_t bytesPerRow = bytesPerElement * width;
    size_t surfaceAllocSize = bytesPerRow * height;

    NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithBool:YES], kIOSurfaceIsGlobal,
                                [NSNumber numberWithUnsignedLong:bytesPerElement], kIOSurfaceBytesPerElement,
                                [NSNumber numberWithUnsignedLong:bytesPerRow], kIOSurfaceBytesPerRow,
                                [NSNumber numberWithUnsignedLong:width], kIOSurfaceWidth,
                                [NSNumber numberWithUnsignedLong:height], kIOSurfaceHeight,
                                [NSNumber numberWithUnsignedInt:pixelFormat], kIOSurfacePixelFormat,
                                [NSNumber numberWithUnsignedLong:surfaceAllocSize], kIOSurfaceAllocSize,
                                nil];

    IOSurfaceRef destSurf = IOSurfaceCreate((__bridge CFDictionaryRef)(properties));

    IOSurfaceLock(destSurf, 0, NULL);
    CARenderServerRenderDisplay(0, CFSTR("LCD"), destSurf, 0, 0);
    IOSurfaceUnlock(destSurf, 0, NULL);

    CGDataProviderRef provider =  CGDataProviderCreateWithData(NULL, IOSurfaceGetBaseAddress(destSurf), (width * height * 4), NULL);
    CGImageRef cgImage = CGImageCreate(width, height, 8,
                                       8*4, IOSurfaceGetBytesPerRow(destSurf),
                                       CGColorSpaceCreateDeviceRGB(), kCGImageAlphaNoneSkipFirst |kCGBitmapByteOrder32Little,
                                       provider, NULL, YES, kCGRenderingIntentDefault);
    UIImage *image = [UIImage imageWithCGImage:cgImage];
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}

回答1:

If you are on a Jailbroken environment, you can use the private UIImage method _UICreateScreenUIImage:

OBJC_EXTERN UIImage *_UICreateScreenUIImage(void);

// ...

- (void)takeScreenshot {
    UIImage *screenImage = _UICreateScreenUIImage();
    // do something with your screenshot
}

This method uses CARenderServerRenderDisplay for faster rendering of the entire device screen. It replaces the UICreateScreenImage and UIGetScreenImage methods that were removed in the arm64 version of the iOS 7 SDK.