gpt4 book ai didi

ios - 截取多个 View - Objective-C

转载 作者:行者123 更新时间:2023-12-01 18:20:11 25 4
gpt4 key购买 nike

我有一个基本 View (UIView)和一个背景图像(UIImageView)。 (背景图像不是基本 View 的 subview 。)我在 self.view 上也有更多 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);

这是基本 View :
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];

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

最佳答案

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

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();

关于ios - 截取多个 View - Objective-C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17911160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com