截取多个视图的截图 - Objective-C

时间:2013-07-28 18:02:36

标签: ios objective-c cocoa-touch screenshot

我有一个baseview(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来解决这个问题,但我不想让它重复以适应屏幕。

1 个答案:

答案 0 :(得分: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();