简体   繁体   English

如何截取应用程序窗口的一部分?

[英]How to take a screenshot of a part of an app's window?

I'm trying to save a screenshot of a part of the screen with all views and layers merged. 我正在尝试保存屏幕的一部分屏幕截图,其中所有视图和图层都已合并。 The only way I know how to do this is with the code below. 我知道如何做到这一点的唯一方法是使用下面的代码。

The code below creates an 80x80 square, but with a top left corner at 0,0. 下面的代码创建一个80x80的正方形,但左上角为0,0。

How can I cut a smaller rectangle out of a full page screenshot? 如何从整页截图中剪切出一个较小的矩形? For example I have a 320x480 full window screenshot, but I only need an 80x80 square centered at 160,240? 例如,我有一个320x480的全窗口截图,但我只需要一个以160,240为中心的80x80平方?

UIGraphicsBeginImageContext(smallView.frame.size);
[appDelegate.window.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Thank you! 谢谢!

after getting the screenshot image crop the required part using this 获取截图图像后使用此方法裁剪所需的部分

CGImageRef imageRef = CGImageCreateWithImageInRect([screenshot CGImage], cropRect);
UIImage *result = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

Untested suggestion... 未经测试的建议......

    UIGraphicsBeginImageContext(smallView.frame.size);
    [appDelegate.window.layer renderInContext:UIGraphicsGetCurrentContext()];

    CGRect cropRect = UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
    CGRectMake(160.0,240.0,smallView.frame.size.width,smallView.frame.size.height);
    CGImageRef croppedImageRef = CGImageCreateWithImageInRect(screenshot.CGImage,cropRect);
    UIImage *croppedScreenshot = [UIImage imageWithCGImage:croppedImageRef];
    CGImageRelease(croppedImageRef);
    UIGraphicsEndImageContext();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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