gpt4 book ai didi

ios - 如何在越狱的 iOS 设备上截取整个屏幕?

转载 作者:行者123 更新时间:2023-11-28 21:37:23 25 4
gpt4 key购买 nike

我需要截取包括状态栏在内的整个屏幕的屏幕截图,我使用 CARenderServerRenderDisplay 来实现此目的,它在 iPad 上可以正常工作,但在 iPhone 6 Plus 上会出错。作为代码中*标记的部分,如果我设置width=screenSize.width*scaleheight=screenSize.height*scale,它会导致崩溃,如果我只是将它们更改为:width=screenSize.height*scaleheight=screenSize.width*scale,它会起作用,但会产生这样的图像: enter image description here ,我已经尝试了很多但没有找到原因,有人知道吗?我希望我已经描述得够清楚了。

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

最佳答案

如果您处于越狱环境中,则可以使用私有(private)UIImage 方法_UICreateScreenUIImage :

OBJC_EXTERN UIImage *_UICreateScreenUIImage(void);

// ...

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

此方法使用 CARenderServerRenderDisplay 来更快地呈现整个设备屏幕。它取代了 iOS 7 SDK 的 arm64 版本中删除的 UICreateScreenImageUIGetScreenImage 方法。

关于ios - 如何在越狱的 iOS 设备上截取整个屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33369014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com