gpt4 book ai didi

ios - 使用阴影设置从 UIView 生成图像

转载 作者:行者123 更新时间:2023-11-29 05:40:48 26 4
gpt4 key购买 nike

我正在尝试生成 UIImage 作为 UIView 的快照。

    CGFloat length = 100;
CGFloat radius = 20;

self.bgView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, length, length)];
self.bgView.backgroundColor = [UIColor whiteColor];
self.bgView.layer.cornerRadius = 5;
self.bgView.layer.shadowRadius = radius;
self.bgView.layer.shadowOpacity = 0.95;
self.bgView.layer.shadowColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5].CGColor;

[self.view addSubview:self.bgView];

CGFloat contextLength = length + radius * 2;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(contextLength, contextLength), NO, 0.0);
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), radius, radius);
[self.bgView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

self.secondBgView = [[UIImageView alloc] initWithFrame:CGRectMake(80, 260, contextLength, contextLength)];
self.secondBgView.image = image;
[self.view addSubview:self.secondBgView];

但是,生成图像的阴影部分是模糊的。

snapshot

如何获得像第一个一样清晰的图像?

最佳答案

深入研究项目https://github.com/uber/ios-snapshot-test-case后,我发现UIView的drawViewHierarchyInRect:drawViewHierarchyInRect:可以工作。

UIView *wrapperView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 320, 150)];
[wrapperView addSubview:self.bgView];
[self.view addSubview:wrapperView];

// ...

[wrapperView drawViewHierarchyInRect:wrapperView.bounds afterScreenUpdates:YES];

关于ios - 使用阴影设置从 UIView 生成图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56576188/

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