1

我有一个基本视图(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 来解决这个问题,但我不希望重复它以适应屏幕。

4

1 回答 1

4

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

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();
于 2013-07-29T02:37:43.997 回答