在iOS中截取屏幕特定部分的屏幕截图

时间:2014-05-11 20:22:55

标签: ios objective-c screenshot

我有一个带有按钮的视图控制器,按下时会截取整个屏幕的截图:

@implementation ViewController
-(IBAction)Screenshot {
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshotimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(screenshotimage, nil, nil, nil);
}
@end

问题:我应该如何获得屏幕特定部分的屏幕截图?

即。我需要屏幕截图是屏幕中的特定矩形而不是整个屏幕

有什么建议吗?

由于

2 个答案:

答案 0 :(得分:1)

您有两种选择:

  • 您说的是[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];而是说[otherview.layer renderInContext:UIGraphicsGetCurrentContext()]; otherview其中{{1}}是您提前建立的视图,其中包含作为子视图的所有部分屏幕你想截取屏幕截图。

  • 或者,按照您的方式进行操作,但将生成的屏幕截图图像裁剪(在代码中)到您想要的区域。

答案 1 :(得分:0)

使用您需要的矩形大小创建图像上下文,然后"移动"在调用CGContextTranslateCTM之前使用renderInContext:围绕坐标系的原点,将屏幕的特定部分绘制到该上下文中。

相关问题