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

Ale*_*one 2 iphone screenshot objective-c uiview ios

我正在尝试保存屏幕的一部分屏幕截图,其中所有视图和图层都已合并.我知道如何做到这一点的唯一方法是使用下面的代码.

下面的代码创建一个80x80的正方形,但左上角为0,0.

如何从整页截图中剪切出一个较小的矩形?例如,我有一个320x480的全窗口截图,但我只需要一个以160,240为中心的80x80平方?

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

UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)

谢谢!

小智 9

获取截图图像后使用此方法裁剪所需的部分

CGImageRef imageRef = CGImageCreateWithImageInRect([screenshot CGImage], cropRect);
UIImage *result = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
Run Code Online (Sandbox Code Playgroud)