【问题标题】:take screenshot of multiple views – Objective-C截取多个视图 - Objective-C
【发布时间】:2013-07-28 18:02:35
【问题描述】:

我有一个基本视图 (UIView) 和一个背景图像 (UIImageView)。 (背景图像不是基本视图的子视图。)我也有更多关于self.view 的视图,我不想被包括在内。当一个按钮被点击时,我的截图是这样的:

CGRect rect = [baseView bounds];
UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[baseView.layer renderInContext:context];
UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(capturedImage, self, @selector(image:didFinishSavingWithError:contextInfo:),nil);

这是基本视图:

baseView = [[UIView alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.origin.x), ([UIScreen mainScreen].bounds.origin.y), ([UIScreen mainScreen].bounds.size.width), ([UIScreen mainScreen].bounds.size.height))];
baseView.backgroundColor = [UIColor clearColor];
[self.view addSubview:baseView];

这是背景图片:

backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height)];
backgroundImage.backgroundColor = [UIColor grayColor];
[self.view addSubview:backgroundImage];
[self.view sendSubviewToBack:backgroundImage];

屏幕截图显然只捕获了基本视图,但我也希望在图像中捕获背景图像。有没有办法做到这一点?
我可以通过将背景图像添加为 baseview 的 backgroundColor 来解决此问题,但我不希望它重复以适合屏幕。

【问题讨论】:

  • 如果你的 self.view 上只有这两个子视图,那么从 self.view 截取屏幕截图
  • @MuhammadZeeshan 但是我对 self.view 有更多的看法,我不想被包括在内,抱歉我没有在问题中提到这一点
  • 暂时将不需要的视图设置为隐藏,截屏,然后重新打开。
  • @RobvanderVeer 谢谢你做得很好!

标签: ios objective-c cocoa-touch screenshot


【解决方案1】:

只需在上下文中渲染两者:

CGRect rect = [baseView bounds];
UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[backgroundImage.layer renderInContext:context];
[baseView.layer renderInContext:context];
UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    相关资源
    最近更新 更多