屏幕截图与框架

时间:2013-02-08 06:30:35

标签: ios

如果要捕获屏幕是ios app,可以使用以下代码:

UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
currentCaptureImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

但我想从特定点捕获图片,例如(start.x,start.y)并且具有特定的宽度和高度,我该怎么做?

1 个答案:

答案 0 :(得分:0)

我只是谷歌并从How to capture a specific size of the self.view

得到最佳答案
UIGraphicsBeginImageContextWithOptions(CGSizeMake(300, 320), YES, 0.);
    [self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

例如,如果您的视图是600x320,并且您希望捕获宽度中间的300个点,那么您将向左侧翻译150个点:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(300, 320), YES, 0.);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, -150.f, 0.f);
    [self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();