简体   繁体   中英

How to take screenshot of entire screen on a Jailbroken iOS Device?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM